Ryan
Ryan

Reputation: 10049

javascript: add html to end of <span> (noob)

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.

Upvotes: 0

Views: 128

Answers (2)

alexP
alexP

Reputation: 3765

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);

Upvotes: 1

Karim AG
Karim AG

Reputation: 2193

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

Upvotes: 1

Related Questions