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.textContentgets content of any element, including hidden elements,<script></script>and<style></style>elements. -
innerTextonly shows "human-readable" elements. -
innerTextalso triggers a reflow.
Versus HTMLElement.innerHTML #
innerHTMLcan cause XSS attacks as it runs the HTML.Node.textContentstrips 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