Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction Listening for Events with addEventListener()

What are the differences between textContent and innerText HTML? Thanks

What are the differences between textContent and innerText HTML? Thanks

1 Answer

Hi 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:










I hope that helps.

Stay safe and happy coding!

Hi Peter,

Definitely helps, thank you!!!