Rich text box like facebook comment box - javascript

I'm currently working on http://flickogram.com.
I need to put rich comment textbox like facebook has.
Currently, I've developed a jquery plugin and you can find its working on http://jsfiddle.net/mike_ivanenko/k6zH6/3/
You can use the plugin as following.
$(document).ready(function() {
$('#ttt').richtextbox( {
highlights: [
{char:'#', class:'highlighted', markup: 'topic'},
{char:'#', class:'highlighted', markup: 'people'}
]
,change:function(richtextfield, input) {
$('#output').val(input.val());
}
}
);
});
It works pretty well for putting topic by typing hash(#), but I can't find a way to deal with people's name by typing #.
My intention is as followings.
When user types #, drop-down box with search text field will be shown just below the current textbox and user can search for the name.
Then the result will be shown in the dropdown, and on clicking the item will be entered in the main text field. But it should be non-editable. Deleting by [Delete] or [Backspace] key is available.
Any suggestions?
Or please help me out this on github.com/mike-ivanenko/richtextbox
Thank you
Mike

you should check out html5's contenteditable feature
Edit: (fixed)
here's what you need
http://jsfiddle.net/ZQSHT/2/
$('div').keyup(function(){
var test = $(this).text();
if( test.indexOf('#') >= 0){
// Found world
alert('#');
$('input').focus();
}
});
$('input').change(function(){
alert( $('input').val() );
$('div').append('#'+$('input').val()+'');
});

Related

How do I keep a previous value in a textbox when using a checkbox to add information to the textbox?

Okay, so I created this form that I use for work and it works pretty well considering my skill level is most definitely not professional. I learned HTML and JavaScript for a couple years in high school and have been self-taught on a lot of things since. Here's what I'm trying to do:
I have my form set up so that if I select an item from a drop-down menu and click a checkbox, the canned response I created is generated in the textbox. However, if I wrote anything in the textbox in advance, it gets wiped out. Now, the way I learned how to do this was based off of self-taught stuff I found online, so this is an example of what I have for the function that gets my canned responses:
function FillDetails29(f) {
if(f.checkbox29.checked == true) {
f.TEXT.value = ('' + f.trans.value + '')
} else {
f.TEXT.value = "";
}
}
I know that having
} else {
f.TEXT.value = "";
is going to wipe out anything that was there before or after if I uncheck the checkbox.
My question is what should I be doing to maintain my previous value when I uncheck the box? Example being:
Previous value to using the checkbox: Andrea looked good in that sweater.
Using the checkbox: Andrea looked good in that sweater. I wonder if there are any more at the store?
Unchecking the checkbox: Andrea looked good in that sweater.
I've done a lot of searching to see if there's something out there that can solve my problem but I'm afraid I'm not phrasing it right when I google it. Can anyone help me or point me in the right direction for this? I know that you guys don't want to just solve it for me and that I should be able to present some kind of example of what I've done to fix the problem but I've tried so many things that haven't worked that it would take too long to list them all without causing some kind of confusion. Even if you just have a website that you know of with an example of this that you can provide me, I'd be very grateful. Thank you!
Edit 1: To clarify, my original setup actually contains 3 forms. One form is for data entry where I input caller information and the checkbox for that spits out the entered data into a singular line of details for when I copy and paste into another program.
The second form is where I have quite a few checkboxes that I use because each section of the form requires separate canned responses. I work for a health insurance company on the phones with doctors offices (and soon I'll be talking to members as well) and I created the form to shorten the amount of time it takes for me to document information. So I have checkboxes that generate data for specific benefits, eligibility, authorizations, transferring the call, etc.
I have a lot of checkboxes to contend with. About 32, by my count. More, if I need to add them. Most of these checkboxes are connected with drop-down menus with the necessary canned response for it. Some of them are connected to their own textbox where I need to enter some kind of pre-determined data, such as a date of birth or a doctor's name. Those are not the focus, though. Once I enter data or select an option from the drop-down and click the corresponding checkbox, the data from that selected option appears in a main text area so that I can copy and paste the response to the work program.
The third form is one that's generated for claims information and has 10 checkboxes on it.
So, if you require more examples of what I'm referring to, I can provide them but it will take a few minutes for me to scrub the work related data that out of the canned responses I created.
Edit 2: The response I got from Epascarello was extremely helpful and I've been trying to experiment with different ways to keep the previous value at the start of the new text being inserted from the checkbox with no luck in getting what I'm looking for, though something unexpected has happened when I start with an empty box and select an option after I altered the code he suggested to this:
function FillDetails29(f) {
const elem = f.TEXT;
if (!elem.dataset.prevValue) elem.dataset.prevValue = elem.value;
const updatedValue = f.checkbox29.checked ? f.trans.value : (elem.dataset.prevValue || '') + (f.trans.value);
elem.value = updatedValue;
}
What started to happen is that if the box was blank previously and I selected an option, the option would generate. Then, if I unchecked the box, the option would remain. If I selected a new option, the new option generates. If I then unchecked the box, the first option and the second option would be there.
Example:
First option selected: Andrea looked great in that sweater.
Second option selected: I wonder if it's on sale now?
When unchecked, first option remains until second option is checked. When second option is unchecked, this is what results (from the same drop-down and checkbox): Andrea looked great in that sweater. I wonder if it's on sale now?
Now, I added the same kind of element to another checkbox item in the same area resulting in the code looking like this for that section:
function FillDetails28(f) {
const elem = f.TEXT;
if (!elem.dataset.prevValue) elem.dataset.prevValue = elem.value;
const updatedValue = f.checkbox28.checked ? f.dental.value : (elem.dataset.prevValue || '') + (f.dental.value);
elem.value = updatedValue;
}
function FillDetails29(f) {
const elem = f.TEXT;
if (!elem.dataset.prevValue) elem.dataset.prevValue = elem.value;
const updatedValue = f.checkbox29.checked ? f.trans.value : (elem.dataset.prevValue || '') + (f.trans.value);
elem.value = updatedValue;
}
And if I do something similar there, checking box 28 and then checking box 29, only whatever was most recently checked will materialize there. However, once everything is unchecked, each selected option will appear in the text box.
Example:
Checkbox 28 selected: Steven doesn't look good today.
Text area shows: Steven doesn't look good today.
Checkbox 29 selected: Andrea looks good in that sweater.
Text area shows: Andrea looks good in that sweater.
Checkbox 28 unselected with 29 still selected, text area shows: Steven doesn't look good today. Steven doesn't look good today.
Checkbox 28 and 29 now unselected, text area shows: Steven doesn't look good today. Andrea looks good in that sweater.
How should I be fashioning this so that those two options materialize one after another when the boxes are checked rather than when they're unchecked?
You can store the value into a data variable and reference it.
function FillDetails29(f) {
const elem = f.TEXT;
if (!elem.dataset.prevValue) elem.dataset.prevValue = elem.value;
const updatedValue = f.checkbox29.checked ? f.trans.value : (elem.dataset.prevValue || '');
elem.value = updatedValue;
}

JQuery (Mobile) / JavaScript Select option cannot be selected

I have created a search filter that detaches the options that does not match with what the user has entered.
For example, the list contains: "hello", "good bye", "halo", and if I enter "h" then it'll only show "hello" and "halo".
The problem I'm facing is that if there is one option I cannot select it, but if there are multiple options I can.
Here is the (full code) JSFiddle: JSFIDDLE LINK
To recreate the problem: Enter "another" in the input box, and try select the "do another" option in the select menu.
I can select any option if there are more than one, so if I enter the word, "do", then I can select either options with that word inside.
var probOptions = $("#problem>option");
function searchFilter() {
$("#searchInput").change(function() {
var searchString = $(this).val().toLowerCase();
$("#problem").empty().append(probOptions);
$("#problem>option").each(function() {
var text = $(this).text().toLowerCase();
if (text.indexOf(searchString) <= -1) {
$(this).detach();
// THE LINE BELOW FIXES THE PROBLEM (THE LINE BELOW IS NEWLY ADDED)
$("#problem").selectmenu("refresh");
}
});
});
}
searchFilter();
I managed to fix the problem myself.
Apparently for JQuery mobile you need to refresh the list, but standard JQuery you don't need to.
Here's the solution (where #problem is the ID of the select menu):
$("#problem").selectmenu("refresh");
Hope this helps anyone that have this problem.

Why textarea freezes when using jQuery function append to insert text

I'm trying to insert something in a text area (#note_text) from a drop down list (#note_template). User should be able to insert a number of items into the text area in between typing characters using the keyboard. Similar to selecting from a collection of emoticons to be put in the message.
When done in the followign way, the text area can receive items after typing.
function update1() {
$txt = $("#note_text").val() + $("#note_template option:selected").val() ;
$("#note_text").val($txt);
$("#note_template").val("");
}
But when done in the following way, the content of the text area wouldn't update after typing. Only before typing I can insert items from the drop down list.
function noteTempalteSelected() {
$("#note_text").append( $("#note_template option:selected").val() );
$("#note_template").val("");
}
So it seems that the use of append causes the text area to freeze. Could anyone explain why ? Thank you.
Ok I think I know whats going on here. For the append to work properly, the element should have an innerHTML property or html() in jquery. Textarea just like the input has a val() property. So for this to work properly you should try this:
$('#note_template').on('click', 'option:selected', function(e){
var txtArea = $('#note_text');
txtArea.val(txtArea.val() + $(this).val());
$(this).val('');
});
[DEMO HERE][1]

Using JQuery MaskInput Plugin with a SSN Field

I am trying to use the JQuery MaskInput plugin on a SSN field but I dont see anyway to make it display "***-**-****" after the user leaves the fields.
Did anyone get this working>
I havent tested this, but it may get you headed in the right direction. The masked input plugin has a completed function that can be called when the user has finished entering their value. You can use it to grab the value and store it in a hidden field to retain it and replace the current text with whatever you desire.
var $txt_SNN = $("#txt_SSN");
$txt_SNN.mask("999-99-9999", {
completed: function() {
$('#hdf_SNN').val($txt_SNN.val());
$txt_SNN.val("999-99-9999");
}
});
$txt_SNN.mask("999-99-9999").blur(function() {
$(this).attr('type', 'password');
});`
This should do it. Although I didn't test it.

Identify URLs in a textarea and show them as output using javascript

i have a text area and a click me. In that text area, if a user enters some URLs then the input should be marked invalid on the button click.
For example, if my input is
"StackOverFlow, the greatest codding buddy you could ever have. The URL: www.stackoverflow.com You can also checkout meta.stackoverflow.com which is also super cool"
At button Click the error should be.
"Form submit failed, because you have entered some URLS. The URLs are
- www.stackoverflow.com
- meta.stackoverflow.com"
I would like a pure javascript solution. No jquery please.
Took one of those regex from the link below:
http://regexlib.com/Search.aspx?k=URL
Then :
var input = document.getElementbyId(id_of_your_form);
var x = input.match(/reg_ex_here/g);
if (x.length>0) { // x[i] will hold the url
var urls=x.concat();
alert(urls);
}
It's not properly tested ... but hope you get the idea..

Categories

Resources