Escaping quotes for innerHTML in Mozilla

T

Ted Weatherly

Hello,

I want to dynamically create a table cell with a textfield in it. The
value for the textfield can have quotes. e.g. ["test" ]

I have this snippet of javascript code:

var td = document.createElement('td');
var cellMarkup = '<input value="&quot;test&quot;">';
td.innerHTML = cellMarkup;
alert("cellMarkup = " + cellMarkup + "\n" +
"td.innerHTML = " + td.innerHTML);

On Mozilla, the text in alert box shows up as:

cellMarkup = <input value="&quot;test&quot;"> <-- Good
td.innerHTML = <input value="test"> <-- Bad

In summary, I am trying to escape the quotes in "test". The escaped
quotes disappear, however, when I set the markup in innerHTML. The
string "test works correctly though. And on IE the escaping the
quotes works all the time.

Does anyone have hints on how to get this to work on Mozilla? I've
already tried escaping with \" and that doesn't work either.

Thanks in advance,
-Ted
 
G

Gary Mayor

Ted said:
Hello,

I want to dynamically create a table cell with a textfield in it. The
value for the textfield can have quotes. e.g. ["test" ]

I have this snippet of javascript code:

var td = document.createElement('td');
var cellMarkup = '<input value="&quot;test&quot;">';
td.innerHTML = cellMarkup;
alert("cellMarkup = " + cellMarkup + "\n" +
"td.innerHTML = " + td.innerHTML);

On Mozilla, the text in alert box shows up as:

cellMarkup = <input value="&quot;test&quot;"> <-- Good
td.innerHTML = <input value="test"> <-- Bad

In summary, I am trying to escape the quotes in "test". The escaped
quotes disappear, however, when I set the markup in innerHTML. The
string "test works correctly though. And on IE the escaping the
quotes works all the time.

Does anyone have hints on how to get this to work on Mozilla? I've
already tried escaping with \" and that doesn't work either.

Thanks in advance,
-Ted

A work around would be to change the " to a ' and then escape it using \
so it comes out as \'. Should work

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top