TIL why you should always use textContent
POSTED ON:
Today I learned the difference between Node.textContent
and all the others.
Versus HTMLElement.innerText
. #
-
Node.textContent
gets content of any element, including hidden elements,<script></script>
and<style></style>
elements. -
innerText
only shows "human-readable" elements. -
innerText
also triggers a reflow.
Versus HTMLElement.innerHTML
#
innerHTML
can cause XSS attacks as it runs the HTML.Node.textContent
strips tags.
var el = document.createElement('div');
el.innerHTML = 'A<p>B<span>C</span>D</p>D';
el.textContent; // "ABCDD" (HTML tags stripped successfully)
via the MDN
via https://stackoverflow.com/a/31466405/4096078
Related TILs
Tagged: js