Reputation: 10964
I have an input box which is populated with a string from an apex method. If I use an apostrophe in the String, I receive an 'Unexpected Identifier' error.
Heres the code:
$('#textInput').append(
$('<input>').addClass('ui-input-note').attr('placeholder', 'Notes')
.val('{! CurrentNote }')
);
This is what displays in the console:
$('#textInput').append(
$('<input>').addClass('ui-input-note').attr('placeholder', 'Notes')
.val('Jim's dog')
);
I'm assuming the apostrophe is exiting the string. How do I get around this?
Other than using " ".
Cheers
Upvotes: 1
Views: 171
Reputation: 30648
You can do it as follow:
catch the escape
character on its key in event
$(document).keypress(function(event){
alert(String.fromCharCode(event.which));
})
append the \ with escape
characters to the user input.
//Append text to input element
function jQ_append(id_of_input, text){
if(text=='`'){
text='\`';
}
$(id_of_input).val($(id_of_input).val() + text);
}
Upvotes: 1
Reputation: 7119
You can use the escape() (and then the unescape() ) function.
Here more info and examples: JavaScript escape() Function
Upvotes: 1