I cannot get rid of & gt; and & lt; using javascript

I raised a few questions, but I couldn't find what I wanted. So here's the code and the problem.

var cell=document.createElement('td');
cell.appendChild( document.createTextNode(unescape('Test <br>')));
alert("Content: " + cell.innerHTML);

      

I want to alert

display as Test <br>

, but instead it displays as Test &lt;br&gt;

. I've tried a lot of things like trying unescape after creating a node, trying unescape when creating a node (see above), trying to notify innerHTML

unescaped but none seem to work. No matter what I tried, I was unable to display the result as I wanted and now I need your help.

+3


source to share


2 answers


What you are doing is creating a textNode <and converting it <and> to html code. You must insert an element if you want the alert to display correctly <br>. Hope this helps you.

http://jsfiddle.net/7b5Yk/



var cell=document.createElement('td');
var br = document.createElement('br');
cell.appendChild( document.createTextNode(unescape('Test')));
cell.appendChild(br)
alert("Content: " + cell.innerHTML);

      

+1


source


Not sure what you really need to do innerHTML

for any reason, but if you change it to innerText

you should get the desired result.

alert("Content: " + cell.innerText);

      



It all seems to depend on whether you really intend to add text or want it to
be an actual line break or not. If you want it to act as a line break in you should most likely go with the Ziinloader approach.

0


source







All Articles