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
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:
innerText wouldn’t return the content of the span but textContent would.
textContent also has better performance than innerText I believe
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 displayedand for security reasons, try to use only 'textContent'... 'innerHTML' can allow malicious scripts to be run
i don't remember about 'innerText' tho
innerText doesn't
it puts everything into the element as text
yep I see, thanks guys !