Escaping quotes for innerHTML in Mozilla

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ted Weatherly

    #1

    Escaping quotes for innerHTML in Mozilla

    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.create Element('td');
    var cellMarkup = '<input value="&quot;te st&quot;">';
    td.innerHTML = cellMarkup;
    alert("cellMark up = " + cellMarkup + "\n" +
    "td.innerHT ML = " + td.innerHTML);

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

    cellMarkup = <input value="&quot;te st&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

    #2
    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.create Element('td');
    > var cellMarkup = '<input value="&quot;te st&quot;">';
    > td.innerHTML = cellMarkup;
    > alert("cellMark up = " + cellMarkup + "\n" +
    > "td.innerHT ML = " + td.innerHTML);
    >
    > On Mozilla, the text in alert box shows up as:
    >
    > cellMarkup = <input value="&quot;te st&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

    Comment

    Working...