Element.after() is an instance method from the ChildNode API. It inserts nodes or strings in the parent’s child list, just after the element you call it on. Learn multi-argument inserts, text coercion, how it compares to insertBefore(), and five try-it labs.
01
Kind
Instance method
02
Inserts
After this element
03
Args
Nodes or strings
04
Returns
undefined
05
Throws
HierarchyRequestError
06
Status
Baseline widely
Fundamentals
Introduction
Older DOM code often used parent.insertBefore(newNode, ref.nextSibling) to place something after a sibling. after() makes that intent clear: call it on the element that should come first, and pass what should follow.
You can pass elements, other nodes, or plain strings. Strings become Text nodes automatically—handy for labels and spacing without creating nodes by hand.
💡
Beginner tip
after() inserts into the parent of the element you call it on. The element itself is the landmark; new content becomes its next sibling(s).
node1, …, nodeN — a set of Node objects or strings to insert. Strings are inserted as equivalent Text nodes.
Return value
None (undefined).
Exceptions
HierarchyRequestErrorDOMException — thrown when the node cannot be inserted at the specified point in the hierarchy (MDN).
Common patterns
JavaScript
const p = document.querySelector("p");
const span = document.createElement("span");
p.after(span); // insert one element
p.after("Text"); // insert a Text node
p.after(span, "Text"); // element, then text
p.after(span, " · ", "more"); // several siblings in order
Cheat Sheet
⚡ Quick Reference
Goal
Code
Insert element after el
el.after(newEl)
Insert text after el
el.after("Hello")
Insert several items
el.after(a, b, "c")
Classic equivalent
parent.insertBefore(node, el.nextSibling)
Return value
undefined
MDN status
Baseline Widely available (since April 2018)
Snapshot
🔍 At a Glance
Four facts to remember about Element.after().
Returns
undefined
Updates the DOM in place
Baseline
widely
Since April 2018
Where
parent list
Next sibling(s) of el
Strings
→ Text
Auto Text nodes
Compare
📋 after() vs insertBefore()
el.after(...)
parent.insertBefore(n, ref)
Called on
The sibling landmark
The parent
Position
Immediately after el
Before ref (or end if null)
Strings
Yes (as Text)
No — need a Node
Multiple inserts
Variadic args
One node per call
Return value
undefined
Inserted node
Best for
Clear “insert after me”
Precise parent + reference
Hands-On
Examples Gallery
Examples follow MDN Element.after() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Insert an element, text, or both—matching MDN’s core demos.
Example 1 — Inserting an Element (MDN)
Place a new <span> immediately after a <p>.
JavaScript
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.after(span);
console.log(container.outerHTML);
// "<div><p></p><span></span></div>"
span becomes the next sibling of p under container. You never call a method on the parent for this case.
Example 2 — Inserting Text (MDN)
Pass a string; the browser creates a Text node for you.
JavaScript
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
p.after("Text");
console.log(container.outerHTML);
// "<div><p></p>Text</div>"
No document.createTextNode needed. The string is inserted as a Text node after p.
📈 Practical Patterns
Combine args, insert several siblings, and move existing nodes.
Example 3 — Element and Text Together (MDN)
Pass an element and a string in one call—order is preserved.
JavaScript
let container = document.createElement("div");
let p = document.createElement("p");
container.appendChild(p);
let span = document.createElement("span");
p.after(span, "Text");
console.log(container.outerHTML);
// "<div><p></p><span></span>Text</div>"
One call inserts three nodes after item: a strong badge, a space Text node, and an empty em you can fill next.
Example 5 — Moving an Existing Node
If the node is already in the document, after() relocates it.
JavaScript
const list = document.getElementById("list");
const a = document.getElementById("a");
const b = document.getElementById("b");
a.after(b);
console.log([...list.children].map((el) => el.id).join(", "));
// "a, b" (b moved to sit after a)
Element.after() 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.after()
Safe for production. Insert nodes or text after an element with clear, modern DOM APIs.
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 · Use insertBefore polyfill pattern
No
after()Excellent
Bottom line: Prefer after() when you think “put this after that element.” Use insertBefore when you already work from the parent and a reference child. Strings become Text nodes automatically.
Wrap Up
Conclusion
Element.after() is the modern way to insert siblings after an element—nodes, text, or several at once. It returns undefined and updates the parent’s child list in place.
Confuse sibling insert with appending inside the element
Assume IE supports after() without a fallback
Forget that invalid trees can throw HierarchyRequestError
Clone by accident—moves remove the old location
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.after()
Insert siblings after an element—nodes or text, one call.
5
Core concepts
📝01
Returns
undefined
API
📄02
Where
after this el
Sibling
✍️03
Strings
become Text
Coerce
✅04
Baseline
widely available
Status
⚡05
Existing
moves
DOM
❓ Frequently Asked Questions
It inserts one or more Node objects or strings into the children of this element’s parent, immediately after this element. Strings become Text nodes.
No. MDN marks Element.after() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
Nothing useful — the return value is undefined. The DOM is updated in place; check parent.outerHTML or nextSibling if you need to verify the result.
Yes. after(node1, node2, …) inserts all arguments in order, right after the element you call it on.
after() is called on the sibling you want to place content after. insertBefore() is called on the parent and needs a reference child. Modern after() also accepts strings as Text nodes.
If the element is not in a parent’s child list, after() has nowhere to insert. Invalid hierarchy cases can throw a HierarchyRequestError DOMException.
Did you know?
after() comes from the ChildNode mixin—the same family as before(), remove(), and replaceWith(). That is why you call it on the sibling you care about, not on the parent container.