In JavaScript, the insertAdjacentHTML() method parses an HTML (or XML) string and inserts the resulting nodes at a position relative to the element (MDN).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.insertAdjacentHTML. Start with this line.
JavaScript
el.insertAdjacentHTML(position, input)
Call
What it does
el.insertAdjacentHTML(position, input)
Primary signature.
el.insertAdjacentHTML(...)
Call with the arguments your case needs.
Built into the browser. Call insertAdjacentHTML(...) on an Element — no import required.
Library declarations
JavaScript
el.insertAdjacentHTML(position, input)
Parameters
Parameter
Description
position
Required. string. Where to insert relative to the target
input
Required. string | TrustedHTML. HTML/XML to parse (MDN)
Return Value
Return Value
Description
*
No useful return value
JavaScript Examples
Example 1 — beforeend
Parse a small HTML string and append it as the last child.
JavaScript
const list = document.getElementById("list");
list.insertAdjacentHTML("beforeend", "<li>C</li>");
console.log(list.textContent);
foo<em> via HTML</em><strong> via Element</strong>
How It Works
insertAdjacentHTML. Parses a string into nodes (MDN).
insertAdjacentElement. Places a ready Element — no parse step.
Same positions. Both share the four relative position names.
Pick by input type. String markup vs node you already built.
Compatibility
Browser Support
Element.insertAdjacentHTML() is Baseline Widely available (MDN: across browsers since April 2018). Safe for modern teaching and production code — with XSS hygiene.
✓ Baseline · Widely available
insertAdjacentHTML()
Parse an HTML string and insert the resulting nodes at a relative position.
WideBaseline
Google ChromeFull support
Supported
Mozilla FirefoxFull support
Supported
Apple SafariFull support
Supported
Microsoft EdgeFull support
Supported
OperaFull support
Supported
Internet Explorer4+
Supported
insertAdjacentHTML()Widely available
Bottom line: Use insertAdjacentHTML for trusted markup inserts; never pass raw user HTML.
Frequently Asked Questions
It parses an HTML or XML string and inserts the resulting nodes at a position relative to the element (MDN). Positions: beforebegin, afterbegin, beforeend, afterend.
No. MDN marks Element.insertAdjacentHTML() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
Nothing useful — the return value is undefined (MDN).
insertAdjacentHTML() takes an HTML string and parses it. insertAdjacentElement() inserts an existing Element node.
MDN warns it is an injection sink (XSS risk). Never pass untrusted strings directly. Sanitize input, use Trusted Types, or use insertAdjacentText() for plain text.
MDN: it does not reparse the element it is called on, so existing children are not corrupted. It is often faster than replacing innerHTML on the parent.