Table of Contents

JavaScript insertAfter method is a common name for a DOM operation, but JavaScript has no native insertAfter() method. To place a node after another node, use parentNode.insertBefore(newNode, referenceNode.nextSibling). The key detail is that nextSibling is null when the reference node is last; insertBefore(newNode, null) correctly appends the new node at the end.

Syntax

parentNode.insertBefore(newNode, referenceNode.nextSibling);

insertBefore() inserts a node immediately before its reference node. By passing the reference node’s next sibling, the new node lands immediately after the reference node.

Basic example

This runnable example adds a new list item after the item labeled “Two”.

<ul id="items">
  <li>One</li>
  <li id="two">Two</li>
  <li>Three</li>
</ul>

<script>
  const reference = document.querySelector("#two");
  const newItem = document.createElement("li");
  newItem.textContent = "Inserted after Two";

  reference.parentNode.insertBefore(newItem, reference.nextSibling);
</script>

The resulting order is One, Two, Inserted after Two, Three.

Reusable insertAfter() helper

You can define a small helper when you need this operation more than once:

function insertAfter(newNode, referenceNode) {
  if (!referenceNode || !referenceNode.parentNode) {
    throw new Error("The reference node must be attached to a parent.");
  }

  referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  return newNode;
}

Because referenceNode.nextSibling is null at the end of its parent, the same line handles both middle insertion and end insertion. The helper throws a clear error if the reference node is missing or detached.

Using the helper

const list = document.querySelector("#items");
const reference = list.querySelector("li:nth-child(2)");
const extra = document.createElement("li");
extra.textContent = "Another item";

insertAfter(extra, reference);

Insert at the end

If you already know you want to append a node, use append() or appendChild() directly. The helper also appends when the reference is the final child:

const list = document.querySelector("#items");
const lastItem = list.lastElementChild;
const finalItem = document.createElement("li");
finalItem.textContent = "Last item";

insertAfter(finalItem, lastItem);

Here, lastItem.nextSibling is null, so insertBefore(finalItem, null) adds it at the end.

Insert after a text node or comment

nextSibling considers every node, including whitespace text nodes and comments. This is usually what you want when inserting relative to an exact node. If your code should work only with element children, use nextElementSibling and append when it is null:

function insertAfterElement(newElement, referenceElement) {
  const parent = referenceElement.parentElement;
  if (!parent) {
    throw new Error("The reference element must be attached to a parent.");
  }

  const nextElement = referenceElement.nextElementSibling;
  if (nextElement) {
    parent.insertBefore(newElement, nextElement);
  } else {
    parent.append(newElement);
  }
}

This element-only version places the new element after the reference among element children; whitespace and comments do not affect the result.

Moving an existing node

If newNode is already in the document, insertBefore() moves it from its old position before inserting it. It does not create a copy. To duplicate a node, clone it first:

const original = document.querySelector("#item-to-move");
const copy = original.cloneNode(true);
insertAfter(copy, original);

Use cloneNode(true) to copy descendants as well as the element. Cloning does not copy event listeners registered with addEventListener().

Choosing the reference node

Both arguments should be nodes accepted by the parent’s insertBefore() method, and the reference node must belong to that parent. Passing null as the reference node means “insert at the end”; it does not mean “insert after an unknown node.” When selecting a reference with querySelector(), check that it was found before calling the helper.

How the insertion position is determined

The parent node controls where insertion happens. When you call parent.insertBefore(newNode, referenceNode.nextSibling), the browser looks at the reference node’s current next sibling and places the new node immediately before that sibling. This puts it directly after the reference node. If there is no next sibling, the reference node is already at the end, so the null reference tells insertBefore() to append.

Make sure the reference node is still attached when you perform the operation. If another part of your code has removed it, parentNode will be null and there is no parent in which to insert. Also confirm that the node you pass as the insertion reference is a child of the parent on which you call insertBefore(); otherwise the DOM method throws a NotFoundError.

For a simple append, parent.append(newNode) is often clearer. Use the insert-after pattern when the position depends on an existing node. The helper returns the inserted node so you can keep using it, for example to set attributes or attach other behavior after insertion.

Summary

There is no built-in JavaScript insertAfter() method. For a node referenceNode, insert after it with referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling). This works at the end too, because a missing next sibling is null, which makes insertBefore() append.