Element.prototype.insertAdjacentHTML
Parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position
Syntax
element.insertAdjacentHTML(position, text)Parameters
| Parameter | Type | Description |
|---|---|---|
| position | 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend' | The position relative to the element |
| text | string | The HTML string to be parsed and inserted |
Return Value
undefined
Examples
const list = document.querySelector('ul')!
list.insertAdjacentHTML('beforeend', '<li>New Item</li>')const card = document.querySelector('.card')!
card.insertAdjacentHTML('afterbegin', '<div class="badge">New</div>')function addRow(table: HTMLTableElement, cells: string[]) {
const html = '<tr>' + cells.map(c => `<td>${c}</td>`).join('') + '</tr>'
table.tBodies[0].insertAdjacentHTML('beforeend', html)
}Understanding Element.prototype.insertAdjacentHTML
The Element.prototype.insertAdjacentHTML method in JavaScript parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position. It belongs to the Element object and is one of the most widely used methods for working with element values in modern JavaScript and TypeScript applications.
The method signature is element.insertAdjacentHTML(position, text). It accepts 2 parameters: position, text. When called, it returns undefined. Understanding when and how to use insertAdjacentHTML() helps you write more expressive, readable code.
Common use cases for Element.prototype.insertAdjacentHTML include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-innerhtml, dom-append, dom-prepend, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Element.prototype.insertAdjacentHTML is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Element.prototype.innerHTMLGets or sets the HTML markup contained within the element
Element.prototype.appendInserts a set of Node objects or strings after the last child of the Element
Element.prototype.prependInserts a set of Node objects or strings before the first child of the Element
Element.prototype.beforeInserts a set of Node or string objects in the children list of this Element's parent, just before this Element
More Element Methods
Other methods in the Element object
Related Tools
More Element Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.