How to append a HTML string such as
var str = '<p>Just some <span>text</span> here</p>';
to the <div> with the id test?
(Btw div.innerHTML += str; is not acceptable.)
10 Answers
Use insertAdjacentHTML which is supported in all current browsers:
div.insertAdjacentHTML( 'beforeend', str );
The position parameter beforeend will add inside the element, after its last child.
Live demo:
Performance
AppendChild (E) is more than 2x faster than other solutions on chrome and safari, insertAdjacentHTML(F) is fastest on firefox. The innerHTML= (B) (do not confuse with += (A)) is second fast solution on all browsers and it is much more handy than E and F.