Reputation: 12194
I need to show a string like value='dummy'
inside an input type=text
. Which is the correct way to do it?
If i use <input type=text value='+escape(str)+"'>
I get value%3D%27dummy%27
. If I don't escape I do not see anything after the first quote. I would like to see the string in the correct way.
Upvotes: 0
Views: 3800
Reputation: 39270
If your value is surrounded by single quotes then:
"<input type='text' value='"+"John's".replace(/'/igm,"'")+"' />";
If your value is surrounded by double quotes then:
'<input type="text" value="'+'"one two"'.replace(/"/igm,""")+'" />';
If you're not sure:
'<input type="text" value="'+'"one two Jon\'s"'.replace(/"/igm,""").
replace(/'/igm,"'")+'" />';
Upvotes: 1
Reputation: 60747
I suggest you don't use string to define the value. Instead, select the DOM element, and set its value.
// Provided you select your input in the "input" variable
input.value = str
This way, you don't have to bother with escaping/unescaping.
Upvotes: 2
Reputation: 7214
<input type='text' value="value='dummy'"> </input>
edit : sorry I realized this is not what you want, see gillesc answer
Upvotes: 0
Reputation: 10874
You need to use unescape in this case, try
'<input type="text" value="'+unescape(str)+'">'
http://www.w3schools.com/jsref/jsref_unescape.asp
Upvotes: 2