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 trial

JavaScript JavaScript and the DOM (Retiring) Making Changes to the DOM Getting and Setting Text with textContent and innerHTML

S Ananda
S Ananda
9,474 Points

When to use textContent and innerHTML

I'm not sure I understand when to use textContent and when to use innerHTML. I looked at the web pages mentioned, but it didn't give in-depth info. The only real difference I could see is if you want to replace the whole element you would use innerHTML. If you want to just replace the text within an element use textContent. Is that all there is to it?

Joel Kraft
Joel Kraft
Treehouse Guest Teacher

S Ananda,

Did you find an answer to your question? As Jesus said, the main difference between the two is that innerHTML parses the string you assign to it as HTML, and textContent does not. Let us know if you still have questions :)

Joel

S Ananda
S Ananda
9,474 Points

Yes, Jesus helped me to understand the concept. Sorry I didn't reply earlier.

Joel Kraft
Joel Kraft
Treehouse Guest Teacher

No problem! Just glad it makes more sense now. :)

1 Answer

Jesus Mendoza
Jesus Mendoza
23,289 Points

Hey Ananda,

In my opinion, you should use textContent when working with text and innerHTML when manipulating the inner HTML of an element.

If you try to add a <p> element using textContent, it will render it as text. However, if you add a <p> element using innerHTML, it will be rendered as HTML.

Good luck!

What is the difference between render as text OR render as HTML?