0

I am sure there must be an easier way that doing this:

foo.innerHTML +="<br>";

The above is producing dirty results as there is a textbox in the SPAN and the data from that textbox gets wiped away when using innerHTML.

I am trying to "re-learn" JS so forgive me if the question is too noobish.

2
  • Pls show your html too. Commented Apr 1, 2014 at 13:18
  • 2
    try using appendChild Commented Apr 1, 2014 at 13:20

2 Answers 2

1

I think this is the neatest way to append a new HTML-Element to another:

HTML

<span id="mySpan">This is my Text</span>

JS

var span = document.getElementById('mySpan');
var br = document.createElement('br');
span.appendChild(br);
Sign up to request clarification or add additional context in comments.

Comments

1

Try var br = document.createElement("br"); foo.appendChild(br);

3 Comments

This won't work. appendChild needs a node as parameter not a string.
it works but I get this error: Uncaught NotFoundError: Failed to execute 'appendChild' on 'Node': The new child element is null.
Thanks, while you were editing I tried Alex's answer and it worked... i'm upvoting both of you though :)

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.