Forum Moderators: open

Message Too Old, No Replies

Browser doesn't interpret HTML when text is replaced

         

paperwings

9:46 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



Hi,

I've searched around, and though this seems like a FAQ, I can't find anything on it. The question is simple. I'm using nodeValue to change the content of a text node. That works just fine. However, if the text I replace contains any HTML, that HTML is printed out verbatim, rather than being interpreted.

For example:


commentArray[0] = "This is a comment. Read more <a href=\"http:\\www.website.com/place\">here</a>.";

....

document.getElementById('commenttext').childNodes[0].nodeValue = commentArray[0];

....

<div>
Comments: <span id="commenttext">Blah blah.</span>
</div>

Prints out the html verbatim (without the backslashes, of course). Is this normal, and is there a way around it.

lavazza

10:15 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



Try wrapping your string in single quotes:

ETA: and use the right sort of slashes in the URI

= "This is a comment. Read more <a href=\"http:\\www.website.com/place\">here</a>.";

= 'This is a comment. Read more <a href="http://www.website.com/place">here</a>.';

fside

10:50 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



Didn't work at all, for me.

Why not just use - document.getElementById('commenttext').innerHTML = commentArray[0];

paperwings

4:48 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



I actually did use the correct slashes in my code. That was just a typo here.

paperwings

6:17 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



I tried the single quotes. That seemed to gum things up elsewhere. This is part of an image browser, and using unescaped quotes seems to mess up the back and next functionality.

paperwings

7:47 pm on Feb 15, 2008 (gmt 0)

10+ Year Member



InnerHTML worked. I think I had originally used innerHTML, and then switched it out because it wasn't part of the W3C DOM. Ignore the post above about the single quotes. That was related to something else. But even after I fixed the problem, single quotes didn't work (as far to achieve my goal.

paperwings

11:59 pm on Feb 18, 2008 (gmt 0)

10+ Year Member



Okay, okay, I figured this one out. I was pulling the first comment out of the database with php and plunking it down. The slashes got put down, too. The fix was to use stripslashes() on the initial comment. Odd convoluted bug.