Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHanwen Zhang
20,084 PointsWhat are the differences between textContent and innerText HTML? Thanks
What are the differences between textContent and innerText HTML? Thanks
1 Answer
Peter Vann
36,427 PointsHi Hanwen!
This is a gross oversimplification, but think of textContent as meant to be used for embedding text in an element, whereas innerHTML can be any HTML, including text, but also including other elements.
I will include more info for a more thorough explanation, though.
One important thing to note: use textContent instead of innerHTML, if all you need to imbed is text
innerHTML can introduce vulnerabilities into your code and can open your apps up to XSS attacks (usually in cases where user input gets store in a node's innerHTML)!
I recommend eventually taking this course:
https://teamtreehouse.com/library/owasp-top-10-vulnerabilities (includes info on XSS attacks)
More info:
https://betterprogramming.pub/whats-best-innertext-vs-innerhtml-vs-textcontent-903ebc43a3fc
https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
https://stackoverflow.com/questions/21311299/nodevalue-vs-innerhtml-and-textcontent-how-to-choose
https://skillforge.com/innerhtml-vs-textcontent-the-difference-between-these-javascript-properties/
https://www.veracode.com/security/xss
I hope that helps.
Stay safe and happy coding!
Hanwen Zhang
20,084 PointsHanwen Zhang
20,084 PointsHi Peter,
Definitely helps, thank you!!!