Handle apostrophe in input value with JavaScript

Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
Marius
Posts: 107

Handle apostrophe in input value with JavaScript

I have a JavaScript code that adds an input field for a user:

Code: Select all

var user = "O'Conner, John";   
b.innerHTML += "<input type='hidden' value='" + user + "'>";
When its inserted it looks like this:

Code: Select all

<input type="hidden" value="O" Conner, John'>
How do I amend this so it outputs like this:

Code: Select all

<input type="hidden" value="O'Conner, John">
I need the value to show the full name with the apostrophe. How can I get this to work?

Admin Posts: 805
You can replace the character with its HTML entity.

As for ' - It can either be &rsquo; or &lsquo;

Code: Select all

var user = "O'Conner, John";
user = user.replace("'", "&lsquo;");
b.innerHTML += "<input type='text' value='" + user + "'>";

Similar Topics