Cris
Cris

Reputation: 12194

How to properly show quotes in HTML input text

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

Answers (4)

HMR
HMR

Reputation: 39270

If your value is surrounded by single quotes then:

"<input type='text' value='"+"John's".replace(/'/igm,"&apos;")+"' />";

If your value is surrounded by double quotes then:

'<input type="text" value="'+'"one two"'.replace(/"/igm,"&quot;")+'" />';

If you're not sure:

'<input type="text" value="'+'"one two Jon\'s"'.replace(/"/igm,"&quot;").
   replace(/'/igm,"&apos;")+'" />';

Upvotes: 1

Florian Margaine
Florian Margaine

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

jazzytomato
jazzytomato

Reputation: 7214

    <input type='text' value="value='dummy'">   </input>

edit : sorry I realized this is not what you want, see gillesc answer

Upvotes: 0

GillesC
GillesC

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

Related Questions