JavaScript Element after() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

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

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).

This page is part of JavaScript Element. Related DOM helpers include insertBefore() and appendChild().

Understanding the after() Method

Calling el.after(...nodes) does not add children inside el. It updates el’s parent so the new nodes sit immediately after el among siblings.

  • It is an instance method on Element (ChildNode mixin).
  • Arguments can be Node objects or strings (as Text).
  • Multiple arguments insert in the order you pass them.
  • Existing nodes in the document are moved, not cloned.
  • Return value is undefined—inspect the DOM to confirm.

📝 Syntax

General forms of Element.after (MDN):

JavaScript
after(node1)
after(node1, node2)
after(node1, node2, /* …, */ nodeN)

Parameters

  • node1, …, nodeN — a set of Node objects or strings to insert. Strings are inserted as equivalent Text nodes.

Return value

None (undefined).

Exceptions

  • HierarchyRequestError DOMException — 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

⚡ Quick Reference

GoalCode
Insert element after elel.after(newEl)
Insert text after elel.after("Hello")
Insert several itemsel.after(a, b, "c")
Classic equivalentparent.insertBefore(node, el.nextSibling)
Return valueundefined
MDN statusBaseline Widely available (since April 2018)

🔍 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

📋 after() vs insertBefore()

el.after(...)parent.insertBefore(n, ref)
Called onThe sibling landmarkThe parent
PositionImmediately after elBefore ref (or end if null)
StringsYes (as Text)No — need a Node
Multiple insertsVariadic argsOne node per call
Return valueundefinedInserted node
Best forClear “insert after me”Precise parent + reference

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>"
Try It Yourself

How It Works

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>"
Try It Yourself

How It Works

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>"
Try It Yourself

How It Works

First span, then the text “Text”—both appear after p in that order.

Example 4 — Several Siblings in One Call

Build a small trail of nodes after a heading or list item.

JavaScript
const item = document.getElementById("item");
const badge = document.createElement("strong");
badge.textContent = "NEW";
const em = document.createElement("em");

item.after(badge, " ", em);
em.textContent = "sale";

console.log(item.parentElement.innerHTML);
// <li id="item">Shoes</li><strong>NEW</strong> <em>sale</em>
Try It Yourself

How It Works

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)
Try It Yourself

How It Works

Like appendChild / insertBefore, a node cannot live in two places. Use cloneNode(true) if you need a copy instead of a move.

🚀 Common Use Cases

  • Inserting a badge, icon, or label right after a title or list item.
  • Adding helper text or separators without wrapping extra containers.
  • Reordering siblings by moving an existing node after another.
  • Replacing the old insertBefore(..., nextSibling) pattern with clearer code.
  • Building small UI trails (element + text + element) in one call.
  • Teaching sibling insertion vs appending children inside an element.

🧠 How after() Inserts Siblings

1

Start from an element with a parent

You call el.after(...) on a node that already has a parent.

Landmark
2

Normalize arguments

Strings become Text nodes; existing nodes may detach from their old place.

Prepare
3

Insert into the parent list

New nodes sit immediately after el, in argument order.

Insert
4

DOM updated; return undefined

Check outerHTML or siblings—there is no return payload.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available since April 2018).
  • after() inserts siblings, not children of the element.
  • Return value is always undefined.
  • Invalid hierarchy can raise HierarchyRequestError.
  • Same ChildNode family: before(), append(), prepend(), replaceWith().
  • Related: insertBefore(), appendChild(), children, JavaScript hub.

Browser Support

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.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Not 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.

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.

Continue with children, insertBefore(), appendChild(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Call after() on the landmark sibling
  • Pass strings when you need Text nodes quickly
  • Pass multiple args to keep insert order clear
  • Expect moves when reusing an existing node
  • Verify with outerHTML or sibling properties

❌ Don’t

  • Expect a returned node (it is undefined)
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.after()

Insert siblings after an element—nodes or text, one call.

5
Core concepts
📄 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.

More Element Topics

Browse Element properties and keep building your DOM skills.

Element hub →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful