Javascript - document.createTextNode()
html, javascript
Solution
Newlines are only significant (AFAIK) within a `<pre>` block.
Outside of that, to force line breaks you'll have to split your string into separate lines and then create a text node followed by a `<br/>` for each one, i.e. something like:
var lines = text.split('\n');
var parent = document.body; // the node you want to insert the string into
for (var i = 0; i < lines.length; ++i) {
parent.appendChild(document.createTextNode(lines[i]));
parent.appendChild(document.createElement('br'));
}
See http://jsfiddle.net/alnitak/WFTD6/
Problem
Using this strips my newLine characters Is there an alternative to this that will render html? ``` function viewCommentToggle( comment ) { theRow = document.getElementById("id"+comment.id); idx = 2; // Comment field cell = theRow.cells[idx]; while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]); element = document.createTextNode(comment.comment); cell.appendChild(element); } ``` This is what Im concered with: ``` element = document.createTextNode(comment.comment); ``` just a fyi....this is what I did and it worked: ``` function viewCommentToggle( comment ) { theRow = document.getElementById("id"+comment.id); idx = 2; // Comment field //cell = theRow.cells[idx]; // while( cell.childNodes.length > 0 ) cell.removeChild(cell.childNodes[0]); cell = $("#id"+comment.id+" > td:eq("+idx+")"); $(cell).empty(); $(cell).html( comment.comment == null ? "" : comment.comment.replace(/\n/g,"<br/>").replace(/\r/g,"") ); ```