The title is pretty clear:
Is there any major difference between innerHTML
and createTextNode
(used with Append
) to fill a span with text?
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.
innerText
/textContent
at least –
Merlinmerlina textContent
, and innerText
are not cross-browser, so one would have to resort to a shim. I'd just use innerHTML
instead. –
Kettie innerContext
, innerText
is aware of CSS styling and will trigger a reflow. developer.mozilla.org/en-US/docs/Web/API/Node.textContent Do not recommend them as interchangeable. –
Filterable Doing some research online, here's what I've found. This should cover it at a high level:
elem.createTextNode(text) and elem.textContent = text do the exact same thing (src: https://javascript.info/task/createtextnode-vs-innerhtml)
While textContent returns the full text contained in a node, innerText returns only the visible text contained in the node. (src: Difference between textContent vs innerText)
My understanding is that certain manipulations of innerHTML remove all bound events, so using createTextNode is preferable.
© 2022 - 2024 — McMap. All rights reserved.