Jeff Noel
Jeff Noel

Reputation: 7618

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

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

Upvotes: 38

Views: 22714

Answers (3)

venkat0591
venkat0591

Reputation: 31

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

Upvotes: 3

jeesty
jeesty

Reputation: 1204

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

Upvotes: 0

Bergi
Bergi

Reputation: 665122

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.

Upvotes: 46

Related Questions