2

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...

3
  • 1
    what is your question?? or issue Commented Jul 5, 2011 at 1:12
  • What about if you set the textContent property ? Commented Jul 5, 2011 at 1:12
  • 2
    Well a new line in HTML is created using the <br /> element, not a "normal" line break. Commented Jul 5, 2011 at 1:15

3 Answers 3

3

Use $("#divtext").html(text);

Instead of $("#divtext").text(text);

Sign up to request clarification or add additional context in comments.

Comments

0

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 .textContent and as explicitly setting the value of a textNode through .nodeValue or .data.

2 Comments

if you have few minutes can you explain me what the OP has asked?
@kobe: Near as I can tell, OP is wondering why the difference. At least that's what I'm going with on this one.
0

The text() method will do an HTML escape. There are some workarounds on the API page, but perhaps the text() method isn't best suited for what you want to do?

http://api.jquery.com/text/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.