Element.insertAdjacentHTML() is an instance method that parses an HTML string and inserts the resulting nodes at one of four positions: beforebegin, afterbegin, beforeend, and afterend. Learn the MDN position diagram, comparison with insertAdjacentElement(), XSS safety basics, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
undefined
03
Args
pos + HTML
04
Parses
HTML/XML
05
Safety
XSS aware
06
Status
Baseline widely
Fundamentals
Introduction
Sometimes you have a small HTML snippet — a list item, a badge, or a paragraph with formatting — and you want to drop it into the page at an exact spot. insertAdjacentHTML(position, htmlString) parses that string and inserts the nodes relative to your target element.
MDN: the method parses the specified input as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It uses the same four position names as insertAdjacentElement().
⚠️
Security note (MDN)
insertAdjacentHTML() is an injection sink. Never pass untrusted user input directly. Sanitize HTML first, or use insertAdjacentText() when you only need plain text.
afterend adds a sibling after the target. MDN: only valid when the element has an element parent in the DOM tree.
Example 5 — Safe vs Unsafe Input
Never pass raw user strings; use trusted markup or plain text APIs (MDN).
JavaScript
const userComment = getUserInput(); // untrusted
// Risky — XSS if userComment contains HTML (MDN)
// box.insertAdjacentHTML("beforeend", userComment);
// Safer for plain text — use insertAdjacentText (MDN)
box.insertAdjacentText("beforeend", userComment);
// Or sanitize HTML with a library before insertAdjacentHTML
Element.insertAdjacentHTML() is Baseline Widely available (MDN: across browsers since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.insertAdjacentHTML()
Parse and insert HTML at beforebegin, afterbegin, beforeend, or afterend — returns undefined.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerNot supported
No
insertAdjacentHTML()Excellent
Bottom line: Use insertAdjacentHTML() for trusted HTML snippets at precise positions. Sanitize user input; prefer insertAdjacentText() for plain text.
Wrap Up
Conclusion
Element.insertAdjacentHTML() parses an HTML string and inserts the resulting nodes at one of four positions around a target element. It is fast and flexible, but requires care with untrusted input.
Use the MDN four-position diagram to pick the right slot
Sanitize HTML from users before inserting (MDN)
Use insertAdjacentText() for plain text comments
Prefer this over rewriting parent innerHTML for partial updates
Keep HTML snippets small and template-like
❌ Don’t
Pass raw user input directly (XSS risk per MDN)
Expect a return value reference to inserted nodes
Use for large page rewrites (consider templating instead)
Confuse afterbegin with beforebegin
Insert into detached nodes with beforebegin / afterend
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.insertAdjacentHTML()
Parse and insert HTML at four positions.
5
Core concepts
📝01
Returns
undefined
void
📄02
Input
HTML str
Parse
📄03
Inside
begin/end
Child
⚠️04
XSS
sanitize
Safe
⚡05
vs
Element
Node
❓ 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 or use insertAdjacentText() for plain text.
MDN: it does not reparse the whole element, so existing children are not corrupted. It is often faster than replacing innerHTML on the parent.
Did you know?
MDN: unlike setting innerHTML on the parent, insertAdjacentHTML() does not reparse existing children inside the target element, which makes partial updates faster and safer for surrounding markup.