innerHTML property

The innerHTML property allows you to retrieve and change an HTML code of an element.

Syntax

javascriptelement.innerHTML;

Example

Let's output an HTML code of an element:

html<p id="elem"><b>text</b></p>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.innerHTML);

The code execution result:

javascript'<b>text</b>'

Example

Let's change an HTML code to a new one:

html<p id="elem"><b>text</b></p>
javascriptlet elem = document.querySelector('#elem');
elem.innerHTML = '<i>!!!</i>';

The code execution result:

html<p id="elem"><i>!!!</i></p>

See also

  • the textContent property
    that contains an element text
  • the outerHTML property
    that contains an outer HTML code of an element