1
var node = document.createTextNode('Hello World');
document.getElementById('main').appendChild(node);

I also want a <br /> between each Hello World

createTextNode is not allowing any html.

Any work around?

1
  • <br> is an element, you'd use createElement to create elements, and elements can't exist inside textNodes, that's why they are called textnodes. Commented Jan 28, 2015 at 23:28

1 Answer 1

2
var node = document.createElement('div');
node.innerHTML = 'Hello World<br>';
document.documentElement.appendChild(node);

You can copy this code to your console and see the result immediately

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

1 Comment

Thanks a lot for this. I was dreading how complicated this was going to get ^_^

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.