When I do:
function testtext()
{
var text = "test line 1\n\
test line 2"
;
$("#divtext").text(text);
}
It all appears on one line. When I do:
function testtext()
{
var text = "test line 1\n\
test line 2"
;
document.getElementById("divtext").innerText = text;
}
It works fine…
If you’re wondering why, it’s because jQuery creates a new text node using
document.createTextNode, passing your string as the argument.This apparently behaves differently than
innerText, though it seems to behave the same as setting.textContentand as explicitly setting the value of a textNode through.nodeValueor.data.