Difference between innerHTML, innerText and textContent and their use cases

Hello guys, can someone explain the difference between innerHTML, innerText and textContent; from what I have understand, innerHMTL reads html tags that can be rendered by the browser but innerText and textContent doesn't.... but is there anything more to that? Should we stick to only one of them or all have their use cases please
5 Replies
snxxwyy
snxxwyy2mo ago
InnerText and textContent read the text inside of a tag. InnerText will only return visible text and textContent will return visible and hidden text. For example, in this case:
<p>I am a paragraph. <span style=“display: none;”>hidden text</span></p>
<p>I am a paragraph. <span style=“display: none;”>hidden text</span></p>
innerText wouldn’t return the content of the span but textContent would. textContent also has better performance than innerText I believe
ἔρως
ἔρως2mo ago
also, textContent returns the content of <script> and <style> tags, but innerText doesnt by the way, using innerText can cause a redraw on the website, because of what snxxwyy said to do not return the hidden text, it has to know what is being displayed
vinicius!
vinicius!2mo ago
and for security reasons, try to use only 'textContent'... 'innerHTML' can allow malicious scripts to be run i don't remember about 'innerText' tho
ἔρως
ἔρως2mo ago
innerText doesn't it puts everything into the element as text
Faker
FakerOP2mo ago
yep I see, thanks guys !
Want results from more Discord servers?
Add your server