Favorites

DOM Element.insertAdjacentHTML() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.insertAdjacentHTML()
DOM Element MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The insertAdjacentHTML() method parses an HTML string and inserts the result at a given position relative to an element, without replacing existing content.

Description

The insertAdjacentHTML() method parses text as HTML and inserts the nodes at one of four positions: before the element, inside it before its first child, inside it after its last child, or after the element. Unlike setting innerHTML, existing content and its event listeners are left alone, and it's faster for adding to a list.

The same XSS warning applies as for innerHTML; don't insert untrusted text. insertAdjacentText() and insertAdjacentElement() insert plain text or an element instead.

"beforebegin" and "afterend" only work when the element has a parent, since the new content is inserted next to the element rather than inside it. Scripts in the inserted HTML don't run. Because existing content isn't re-parsed, it's much faster than adding to innerHTML when appending items to a long list.

Syntax

element.insertAdjacentHTML(position, text)

Parameters

  • positionWhere to insert: "beforebegin", "afterbegin", "beforeend" or "afterend".
  • textThe HTML to parse and insert.

Return Value

None (undefined).

Example

<ul id="list">
  <li>Existing</li>
</ul>
const list = document.getElementById('list');
list.insertAdjacentHTML('beforeend', '<li>Added at the end</li>');
list.insertAdjacentHTML('afterbegin', '<li>Added at the start</li>');
list.insertAdjacentHTML('beforebegin', '<h4>Before the list</h4>');
console.log(list.children.length);

Browser Support

The following information will show you the current browser support for the Element.insertAdjacentHTML() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee