jQuery .insertAfter() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("<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"))
CallWhat it does
Matched setContent to insert (left side)
targetSelector / element / jQuery / HTML string
CallbackNot supported — use .after(fn) instead

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Build or select the content to insert.
Step 2Choose the target (selector, element, or jQuery object).
Step 3Call .insertAfter(target).

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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() Method — Insert Before a Target

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

insertBefore() Method — Insert Before a Target tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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