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

MIHA WANG
MIHA WANG
Front End Web Development Techdegree Student 10,820 Points

InnerHTML and textContent

I already watch the tutorial, but both sides can change text, whats the really difference between this two? Thank you so much.

1 Answer

Kieran Barker
Kieran Barker
14,976 Points

The innerHTML property lets you insert HTML into an element, whereas the textContent property only lets you insert text.

If I had a div element and I set its innerHTML to <h1>Title</h1>, then this new H1 element would be inserted into the element. But if I used textContent, then the H1 opening and closing tags would literally just show as text and would not be rendered as an H1 element in the HTML.