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 trialS Ananda
9,474 PointsWhen 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?
S Ananda
9,474 PointsYes, Jesus helped me to understand the concept. Sorry I didn't reply earlier.
Joel Kraft
Treehouse Guest TeacherNo problem! Just glad it makes more sense now. :)
1 Answer
Jesus Mendoza
23,289 PointsHey 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!
Zara Sales
UX Design Techdegree Student 787 PointsWhat is the difference between render as text OR render as HTML?
Joel Kraft
Treehouse Guest TeacherJoel Kraft
Treehouse Guest TeacherS 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