Varaquilex
Varaquilex

Reputation: 3463

I cannot get rid of > and < using javascript

I looked up some SO questions but I could not find exactly what i wanted. So, here is the code and the problem.

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

I want the alert appear as Test <br> but instead it appears as Test &lt;br&gt;. I tried many things like trying to unescape after the node is created, tried to unescape while creating the node(above), tried alerting the innerHTML unescaped but none seem to have worked. Whatever I tried, I failed to render the output as I desired and now I need your help.

Upvotes: 3

Views: 463

Answers (2)

Peter Karlsson
Peter Karlsson

Reputation: 1180

Not sure if you absolutely need to do innerHTML for some reason but if you change it to innerText you should get the desired result.

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

Seems it all depends on if you are actually intending to add text or if you want that
to be an actual line break or not. If you want it to act as a line break in the td you should most likely go with Ziinloader's approach.

Upvotes: 0

JohannesAndersson
JohannesAndersson

Reputation: 4620

What you are doing is create a textNode < br > and its converting the < and > to html code. You have to insert an element if you want the alert to properly show < br > tag. 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);

Upvotes: 1

Related Questions