0

I cant do new line from javascript, I tried to do that with \n, but nothing.

document.getElementById("text").textContent =  "hhh" + '\n' + "df"; 
<h1 id="text">
  abc
</h1>

2 Answers 2

3

Changing textContent to innerText will do it.

document.getElementById("text").innerText =  "hhh" + '\n' + "df"; 
<h1 id="text">
  abc
</h1>

The line break behavior is standardized.

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

4 Comments

interesting. I saw it for the first time.
Very useful difference vs. textContent.
@T.J.Crowder I think ( I cant remember) that there was another difference maybe with inner tags that are being displayed when you ask for innerText , while not displayed in the other method...but I think it might not relates to this ^.
I know there are several differences. I rarely remember what they are, but this treatment of newlines is a major useful one... :-)
2

Apparently you are pretty new in javascript. You should use <br> instead. \n new line for console outputs. Also you should use innerHTML instead of textContent.

document.getElementById("text").innerHTML =  "hhh" + '<br>' + "df"; 
<h1 id="text">
  abc
</h1>

With textContent

document.getElementById("text").textContent =  "hhh" + '<br>' + "df"; 
<h1 id="text">
  abc
</h1>

Console example:

console.log("abc\ndef");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.