jQuery .insertAfter() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .insertAfter()?

.insertAfter(target) inserts every matched element immediately after the target as a sibling (since jQuery 1.0). Same job as .after(), but content comes first.

Remember
$("<p>Test</p>").insertAfter(".inner")  // create + insert
$("p").insertAfter("#foo")                 // move existing <p>s
$("#note").insertAfter(".box")             // content first

// Same result as: $(".box").after($("#note"))
NeedUse
Content-first after$(content).insertAfter(target)
Anchor-first after$(target).after(content)
Content-first before$(content).insertBefore(target)
Inside as last child$(content).appendTo(target)

How to Use .insertAfter()

Select (or create) content, then place it after a target.

  1. Build or select the content to insert.
  2. Choose the target (selector, element, or jQuery object).
  3. Call .insertAfter(target).
  4. Chain on the returned inserted set if needed.
PieceRequired?Role
Matched setYesContent to insert (left side)
targetYesSelector / element / jQuery / HTML string
CallbackNoNot supported — use .after(fn) instead

Pseudocode

Pseudocode
content = matched elements (or newly created nodes)
targets = resolve(target)

for each target t:
    if first target:
        move content after t          // not cloned
    else:
        clone content after t         // clones for extra targets

return new set of inserted nodes
// (original + clones when multiple targets)

Cheat Sheet

Keep these facts handy while inserting siblings.

TopicDetail
Syntax$(content).insertAfter(target)
SincejQuery 1.0
PositionNext sibling after target
vs afterSame result; content-first vs anchor-first
Move / clone1 target moves; many targets clone
ReturnInserted set (not the target)
CallbackNone — prepare content first

Live Preview

Insert a note after the target box, or move an existing line.

Browser demo of .insertAfter() — same idea as the examples below.

I will move

Target box
Result
Ready — pick an action and Run

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Create and Insert

Build a new node and place it after each .inner.

jQuery
$("<p>Test</p>").insertAfter(".inner");
Try It Yourself

How It Works

  1. Create. $("<p>Test</p>") builds a jQuery set.
  2. Target. .inner selects every anchor.
  3. Insert. Content lands as the next sibling after each target.
  4. Clone. Extra targets get clones of the new paragraph.

Example 2 — Move Existing Paragraphs

Official-style demo: move all p elements after #foo.

jQuery
$("p").insertAfter("#foo");
// same idea as: $("#foo").after($("p"))
Try It Yourself

How It Works

  1. Select content. Every p is the matched set.
  2. One target. #foo is a single anchor.
  3. Move. Paragraphs leave their old place and follow #foo.
  4. Order. Matched paragraphs keep their relative order.

Example 3 — .insertAfter() vs .after()

Same DOM result — only the argument order changes.

jQuery
$("<hr>").insertAfter("#box");
// equivalent:
// $("#box").after("<hr>");
Try It Yourself

How It Works

  1. insertAfter. Content on the left; target on the right.
  2. after. Target on the left; content on the right.
  3. Pick one. Use content-first when you already hold the node.
  4. Result. Both create the same next sibling.

Example 4 — Multiple Targets Clone

One source node is cloned for every extra target.

jQuery
$("#badge").insertAfter(".card");
// 1st .card → moves #badge
// 2nd+ .card → clones of #badge
Try It Yourself

How It Works

  1. First target. The original node is moved after it.
  2. Next targets. jQuery clones for each remaining match.
  3. Return. You get the original plus clones as one set.
  4. IDs. Clones copy id — avoid duplicate IDs in real UIs.

Example 5 — Chain On the Inserted Set

Style the nodes you just inserted (the return value).

jQuery
$("<span>new</span>")
  .insertAfter("#box")
  .css("font-weight", "bold");
Try It Yourself

How It Works

  1. Create. A new span is built.
  2. Insert. .insertAfter("#box") places it.
  3. Return. The chain continues on the inserted span.
  4. Style. .css() updates that returned set.

Edge Cases & Pitfalls

Check these before moving nodes around the tree.

clone

Multiple targets clone

One target moves; many targets clone. Duplicate id values on clones break uniqueness.

callback

No callback argument

Need per-target dynamic markup? Use .after(fn) instead.

xss

HTML strings can execute code

Do not build markup from URL params, cookies, or form fields without escaping.

svg

SVG is not officially supported

Using jQuery DOM insert methods on SVG may behave unexpectedly unless a method documents SVG support.

Browser Support

.insertAfter() has been part of jQuery since 1.0. Return-set behavior for single-target inserts was clarified in 1.9 for reliable .end() chaining.

✓ Stable · jQuery 1.0+

jQuery .insertAfter()

Supported in all browsers that run jQuery 1.0+. Prefer a current jQuery 3.x build for modern projects.

100% jQuery core method
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.insertAfter() Universal

Bottom line: Everyday sibling insert. Prefer content-first when you already hold the node to place.

Performance Notes

ApproachCostBest for
One targetCheap moveRelocating a known node
Many targetsClone + insertSmall lists of anchors
Huge multi-target setsExpensiveBatch or use DocumentFragment patterns

Cloning across dozens of targets multiplies work. Prefer one container update when you can.

Key Takeaways

  • Role: insert content as the next sibling after a target.
  • Syntax: content first — twin of .after().
  • Move/clone: one target moves; many targets clone.
  • Return: the inserted set, ready to chain.

One line: .insertAfter() is content-first sibling insertion after a target — same result as .after() with flipped arguments.

Frequently Asked Questions

.insertAfter(target) inserts every element in the matched set immediately after the target as a sibling. The target can be a selector, DOM element, HTML string, array, or jQuery object. Returns a jQuery collection of the inserted elements. Available since jQuery 1.0.
Same job, reversed syntax. With .after(), the anchor comes first: $('p').after('<hr>'). With .insertAfter(), the content comes first: $('<hr>').insertAfter('p').
.insertAfter() inserts as the next sibling after the target. .insertBefore() inserts as the previous sibling before the target.
.insertAfter() inserts as a sibling after the target (outside). .appendTo() inserts as the last child inside the target.
One target → the element is moved. Multiple targets → clones are created for every target after the first; the returned set is the original plus clones.
No. Unlike .after(), .insertAfter() only accepts a target. Build content first, then call .insertAfter(target).
A new set of the inserted elements (useful with .end() since jQuery 1.9), not the original unrelated collection.

Next: .insertBefore()

Insert matched elements before a target with the same content-first style.

.insertBefore() tutorial →

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