HTML DOM Element innerHTML
Example
Get the HTML content of an element with id="myP":
Change the HTML content of an element with id="demo":
Get the HTML content of a <ul> element with id="myList":
Delete the HTML content of a <p> element with id="demo":
More examples below.
Description
The innerHTML property sets or returns the HTML content (inner HTML) of an element.
The Differences Between
innerHTML, innerText and textContent
See below
Syntax
Return the innerHTML property:
Set the innerHTML property:
Property Value
Return Value
More Examples
Example
Change the HTML content of two elements:
document.getElementById("myP").innerHTML = text;
document.getElementById("myDIV").innerHTML = text;
Example
Repeat the HTML content of an element:
Example
Change the HTML content and URL of a link:
element.href = "https://www.w3schools.com";
The Differences Between
innerHTML, innerText and textContent
The text content of the element, including all spacing and inner HTML tags.
Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements.
The text content of the element and all descendants, with spacing and CSS hidden text, but without tags.
HTML Example
JavaScript Examples
let text = document.getElementById("myP").innerHTML;
let text = document.getElementById("demo").textContent;
In the example above:
This element has extra spacing and contains a span element.
This element has extra spacing and contains <span>a span element</span>.
This element has extra spacing and contains a span element.
Browser Support
element.innerHTML is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | Yes |