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

When would one use the 'innerHTML' method?

Just wanted to know this to have some more context, thank you.

2 Answers

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

You want to use innerHTML if you want to change (or get) the html inside a html-tag - but if you just want to change (or get) the text then use textContent instead

HI baderbouta , We can use the 'innerHtml' method when you want to update or add the content of an HTML element.

document.getElementById("example").innerHTML = "this is an example"; 

..... this will change content of the HTML element with id 'example' to 'this is an example'. We can also use this method to get the content of an HTML element and do some action based on the content .

example :-

var x = document.getElementById("example").innerHTML;