Is there any major difference between innerHTML and using createTextNode to fill a span?

21.3k views Asked by At

The title is pretty clear: Is there any major difference between innerHTML and createTextNode (used with Append) to fill a span with text?

3

There are 3 answers

8
Bergi On BEST ANSWER

Of course. createTextNode will escape any strings and show them as they are, while innerHTML could render html-like strings into a DOM. If you don't want that (unless you are sure the text contains no unescaped tags, e.g. when assigning a literal directly), you can use textContent (or innerText for IE).

Yet I'd recommend createTextNode, because all browsers support it equally without any quirks.

0
jeesty On

My understanding is that certain manipulations of innerHTML remove all bound events, so using createTextNode is preferable.

0
venkat0591 On

Doing some research online, here's what I've found. This should cover it at a high level: