Connecting Tech Pros Worldwide Forums | Help | Site Map

Escaping quotes for innerHTML in Mozilla

Ted Weatherly
Guest
 
Posts: n/a
#1: Jul 20 '05
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
Gary Mayor
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Escaping quotes for innerHTML in Mozilla


Ted Weatherly wrote:[color=blue]
> 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[/color]

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

Gary
Closed Thread