Search code examples
javascriptline-breaks

Trying to make a line break with Javascript


Trying to get a line break with javascript but its not working.

document.getElementById('section').textContent = "Hello world<br>" + msg;

I also tried:

document.getElementById('section').textContent = "Hello world" + /n msg;

Doesn't work either.. am i missing something?


Solution

  • At the moment, you're adding literal text, NOT HTML <br> elements. You'll either want to set the innerHTML of the element...

    document.getElementById('section').innerHTML = "Hello world<br>";
    

    ...or create text and <br> elements and append those to the document.

    var text = document.createTextNode("Hello world"),
        break = document.createElement("br"),
        section = document.getElementById("section");
    section.appendChild(text);
    section.appendChild(section);