0

I'm building a html table using javascript on the fly and I want to add a cell that may contain html markup. I do not want that markup interpreted but simply displayed.

  LogHTML += "<tr><td>" + data + "</td></tr>";

How can I do that? Is there some function or html markup that will accomplish that?

I tried pre but then I get extra spaces.

Mike D
  • 2,753
  • 8
  • 44
  • 77

2 Answers2

1
LogHTML += "<tr><td>"+data.replace(/</g,"&lt;")+"</td></tr>";

That's all you need.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

The best solution is to use DOM instead of innerHTML and to create text node for data.

But you can try this as a quick solution or

function htmlentities(s){
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}
artyom.stv
  • 2,097
  • 1
  • 24
  • 42