Escape My HTML Character - javascript

I have a requirement where if user enters anything in the free text field, it should be written on Page, problem i am facing when user enters any html character, it doesnt write anything, as it takes it as html character instead of normal text.
http://jsfiddle.net/mJwwS/
$('#add').on('click',function(){
$('#hey').html($('#oye').val());
});

Use .text() method instead:
$('#hey').text($('#oye').val());
DEMO: http://jsfiddle.net/mJwwS/1/

Related

How to remove one or more white space before a text in the input field

I have validated input fields to check for empty fields. Later I noticed someone can just enter white space and submit the form. Can someone help me remove white space before text but accept space in the middle of the text of the input. I tried using this but will not allow white space in the middle of the input
$('input').keyup(function() {
$(this).val($(this).val().replace(/\s+/g, ''));
});
How do I fix this?
First of all, nothing to do with your question, but you should be validating the field only when the user is trying to submit your form, so, you should use the HTMLFormElement's submit event.
Second, you should be revalidating your form in your backend, because people can just change things client side, via console/debug etc, and that's a really security matter.
Third, about your question itself: You can use the String.prototype.trim method, as follows:
$('input').keyup(function() {
$(this).val($(this).val().trim());
});
.trim() removes leading and trailing spaces leaving ones in the middle unmolested.
You need trim method. Short example:
var str = " Hello World! ";
alert(str.trim());
The alert will display Hello World without white spaces.
More information here:
http://www.w3schools.com/jsref/jsref_trim_string.asp
you can use String trim() Method
Reference is here

How to avoid escape character while retrieving from textbox/textarea through jquery?

here i'm copying the contents from webpage and pasting the contents in form fields like textarea. while retrieving the form contents, escape characters are also coming along which is breaking the functionality. how to avoid the escape characters and retrieve only the given text. The escape characters are not visible in the form fields, but its visible in json.
the above 1st image is entered text and second image is retrived in json. Help me out in this
Thanks.
Try to remove the line breaks with this line of code:
text = text.replace(/(\n|\r)/gm,"");
Cf: How to remove all line breaks from a string?

How do I bind a non-breakspace(alt-288 or  ) upon pressing the space bar in javascript?

I'm using OpenMRS, it's an opensource medical records system. OpenMRS has a built-in html editor and I use this mostly in javascripting ang building the forms. In one of my forms,I have a textarea. My client would like his entries(in paragraph or in list) to be indented in the textarea.
Now when you try indenting the paragraph in the textarea then save the changes and preview the form, the paragraph becomes justified instead of retaining the indented lines.
However, if I try indenting the paragraph using ascii code for non-break space by typing   or pressing alt-288, the paragraph becomes indented thus giving me the desired result. Now, the users don't prefer typing or pressing ascii equivalents coz that'll be hassle on their part.
I'm using mostly javascript and jQuery because it's what openmrs supports. If I could somehow bind the non-break space character upon pressing a key then this will work, but I'm at a lost here. How will I do this in javascript or jquery?
One solution which might work for you is to replace leading spaces in the textarea when you process/save or even each time it changes it something like :
ta.value = ta.value.replace (/\n +/, function (m) {
return '\n' + Array (m.length).join ('&#160');
});
The Array ... constructs creates an array containing length elements then joins with your non-breakspace character effectively creating a string of that many space chars.
Another possibility is to watch for space characters entering the text-area and transforming them. See here : Can I conditionally change the character entered into an input on keypress?

jquery autocomplete on last word

I'm trying to edit this js code so that the autocomplete function kicks in after each non-breaking space; regardless of whether or not the text entered before each non-breaking space was one of the tags. So, for example, if I enter PHP, that's one of the tags. But, if for the next text that I enter, it must be one of the tags too otherwise anything that I enter after PHP will not activate the autocomplete. You can find this example on jsfiddle
I think that you're looking for a plugin like this one:
http://levycarneiro.com/projects/tag-it/example.html
- or -
http://code.drewwilson.com/entry/autosuggest-jquery-plugin
Basically you need to look for "jQuery Autocomplete Tags" instead of just "jQuery Autocomplete".

Stop Jquery from replacing the field

I almost got what I want now. (Cobbled together from code all over the internet.)
But I need to know how to keep my query from being over written.
Example I have a text field. When a word on the dropdown is selected it puts the text in to the form field. If I select another word it replaces that word. What I want it to do is to be able to select as much as I want and no matter how many words I touch it'll keep those unless I go up and backspace or delete them out of the form field.
Here's my code.
http://pastebin.com/1wnAZMNM
One way is to append the new text to the input control instead of replacing its contents, possibly inserting a space character in-between:
$(".myselect").change(function() {
$(".textbox").val($(".textbox").val() + ' ' + $(this).val());
});

Categories