JavaScript JavaScript and the DOM 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
Henrik Christensen
Python Web Development Techdegree Student 38,297 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;