DOM Element.insertAdjacentHTML() Method
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
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.
Desktop





Tablets & Mobile





