181 of 264 menu

innerHTML property

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

Syntax

element.innerHTML;

Example

Let's output an HTML code of an element:

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

The code execution result:

'<b>text</b>'

Example

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

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

The code execution result:

<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
enru