The .insertAfter() method inserts every matched element immediately after a target as a sibling — content-first syntax, the mirror of .after(). This tutorial covers selectors, DOM elements, HTML strings, move vs clone behavior, comparisons with .after(), .insertBefore(), and .appendTo(), the official jQuery demos, and five practical try-it examples.
01
.insertAfter()
Content first
02
Target
Selector
03
Move
DOM nodes
04
vs .after()
Reversed
05
Sibling
Not child
06
Since 1.0
Core API
Fundamentals
Introduction
When you build new markup and already know what it is but not yet where it goes, .insertAfter() lets you configure the element first, then choose the anchor. Available since jQuery 1.0, it is the content-first counterpart to .after().
Pass a CSS selector, DOM element, HTML string, array, or jQuery object as the target parameter. The matched collection is inserted as the next sibling after each resolved target. Unlike .after(), there is no callback form — build dynamic nodes first, then call .insertAfter().
.insertAfter() and .after() perform the same DOM task with reversed syntax. Use .appendTo() when content belongs inside the target. Browse the jQuery DOM hub for related manipulation methods.
Concept
Understanding the .insertAfter() Method
.insertAfter( target ) takes every element in the current jQuery collection and inserts it immediately after the specified target element(s). The target stays in place; matched elements become its following siblings. Since jQuery 1.9, the return value is the inserted element set — not the original collection.
When moving an existing node after a single target, jQuery moves it rather than cloning. With multiple target elements, clones are created for every target after the first.
💡
Beginner Tip
$("<span> Test </span>").insertAfter(".inner") adds a span after every .inner element — the official jQuery demo. Same result as $(".inner").after("<span> Test </span>") with reversed syntax.
Foundation
📝 Syntax
jQuery .insertAfter() accepts one argument:
Insert after target — since 1.0
jQuery
.insertAfter( target )
target — selector, DOM element, HTML string, array, or jQuery object.
Matched elements insert as the next sibling after each resolved target.
Returns the inserted element collection (since jQuery 1.9 for reliable chaining).
📋 .insertAfter() vs .after() vs .insertBefore() vs .appendTo()
Four related insertion APIs — pick the right position and syntax.
.insertAfter()
content first
Insert matched elements after target — content precedes method: $(el).insertAfter(target)
.after()
target first
Insert content after matched elements — anchor precedes method: $(target).after(content)
.insertBefore()
prev sibling
Insert before target — content first: $(el).insertBefore(target)
.appendTo()
last child
Insert inside target as last child — not a sibling operation
Hands-On
Examples Gallery
Examples 1–3 follow the official jQuery API documentation. Examples 4–5 cover the callback form and multiple-argument insertion from the docs. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demos and reversed-syntax sibling insertion.
Example 1 — Official Demo: Insert Paragraphs After #foo
Before: <p>is what I said...</p> <div id="foo">FOO!</div>
After: <div id="foo">FOO!</div> <p>is what I said...</p>
Paragraphs move after #foo as siblings
How It Works
The matched collection (p elements) is inserted after #foo. Paragraphs disappear from their original positions and become following siblings of the target. Returns the inserted element set since jQuery 1.9.
Example 2 — Official Demo: Insert HTML After .inner
Create markup and insert it after every .inner element — content-first syntax.
jQuery
$( "<span> Test </span>" ).insertAfter( ".inner" );
Each .inner gets <span> Test </span> as next sibling
Official jQuery insertAfter HTML pattern
Equivalent to $(".inner").after("<span> Test </span>")
How It Works
jQuery parses the HTML string into a new element, then inserts it after each resolved .inner target. With multiple targets, clones are created for every target after the first.
📈 Practical Patterns
Moving elements, build-then-insert chains, and multi-target insertion.
Example 3 — Official Demo: Move <h2> After a Container
Relocate an existing heading to follow a layout container — jQuery moves it from its original location.
h2 removed from original location
Inserted as sibling after .container
Single target → element moves (not cloned)
How It Works
When the target is a single element, jQuery moves the matched heading rather than cloning it. The return value is the inserted element set — useful for further chaining on the moved node.
Example 4 — Build Then Insert: Error Message After Field
Create a validation message, configure it, then insert after the field wrapper — content-first reads naturally.
<p class="error">Email is required</p> inserted after #email-wrap
.text() keeps message safe from HTML injection
Chain configuration before choosing destination
How It Works
Because .insertAfter() has no callback, build dynamic siblings first. Chain .text(), .addClass(), or event handlers on the new node, then call .insertAfter() at the end.
Example 5 — Insert After Multiple Targets
Insert a badge after every section heading — jQuery clones for all but the first target.
jQuery
$( "<span class='badge'>New</span>" ).insertAfter( "h3.section-title" );
// Multiple h3.section-title → badge cloned after each (except first uses original)
Badge appears after each h3.section-title
Multiple targets → clones for targets after the first
Return value includes original + clones
How It Works
When the target selector matches multiple elements, jQuery inserts after each one. The first target receives the original node; subsequent targets get clones. This differs from calling .after() on multiple anchors with one content argument.
Applications
🚀 Common Use Cases
Build-then-place — create widgets with $('<div>'), configure them, then .insertAfter() the anchor.
Validation errors — insert error messages as siblings below field wrappers.
Read-more links — place a “Continue reading” link after article excerpts.
Relocate headings — move an existing <h2> to follow a layout container.
Section badges — insert “New” labels after multiple section titles.
Reversed syntax — prefer insertAfter when content is built before the destination is chosen.
🧠 How .insertAfter() Inserts Content
1
Match content to insert
jQuery collection holds the element(s) that will be placed after the target.
Content
2
Resolve target element(s)
Selector, DOM node, or jQuery object passed as the insertAfter argument.
Target
3
Insert as next sibling
Matched elements placed immediately after each resolved target.
After
4
⬇
Return inserted set
Inserted element collection since jQuery 1.9 — chain further on the new nodes.
Important
📝 Notes
Available since jQuery 1.0 — no callback form (build content first).
Inserts as the next sibling after the target; use .insertBefore() for the previous sibling.
Single target moves existing nodes; multiple targets clone after the first.
Do not pass HTML strings from untrusted user input — XSS risk.
.appendTo() inserts inside as last child; .insertAfter() inserts as sibling below.
Since jQuery 1.9, returns the inserted element set for reliable chaining.
Before jQuery 1.9, single-target insertAfter return value made .end() chaining unreliable.
Compatibility
Browser Support
.insertAfter() has been part of jQuery since 1.0+. It relies on standard DOM insertBefore operations on the parent node. jQuery 1.9 fixed the return value for single-target insertion. Works in all browsers supported by your jQuery build — IE6+ through modern evergreen browsers in legacy jQuery versions, and all current browsers in jQuery 3.x and 4.x.
✓ jQuery 1.0+
jQuery .insertAfter()
Universal sibling insertion API with content-first syntax across jQuery 1.x, 2.x, 3.x, and 4.x. Pair with .insertBefore() for above/below placement and .after() when anchor-first syntax reads better.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.insertAfter()Universal
Bottom line: Default choice when you build content first and insert it after a target element.
Wrap Up
Conclusion
The jQuery .insertAfter() method inserts every matched element as the next sibling after a target. Pass a selector, DOM element, HTML string, or jQuery object as the target — exactly as the official jQuery demos demonstrate.
Use .after() when the anchor is already selected, and .insertBefore() when content belongs above the target. For content inside a container, use .appendTo() instead. Next up: the content-first counterpart for inserting above a target with .insertBefore().
Use .insertAfter() when building elements before choosing the anchor
Create messages with .text() for safe dynamic error text
Chain .addClass() and events before calling insertAfter
Prefer .after() when the anchor collection is already selected
Check whether content belongs inside (.appendTo) or outside (.insertAfter)
❌ Don’t
Use .insertAfter() when content should be a child inside the target
Pass untrusted HTML strings to $('<div>') constructors (XSS risk)
Confuse .insertAfter() with .appendTo() (sibling vs child)
Assume inserting always clones — single target moves existing nodes
Expect a callback on insertAfter — build dynamic nodes first
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .insertAfter()
The content-first sibling API — insert after a target with reversed syntax.
6
Core concepts
1st01
Content
First
Syntax
→02
Target
Argument
Anchor
↔03
vs after
Reversed
Compare
1.904
Returns
Inserted
Chain
↔05
vs appendTo
Sibling
Scope
×N06
Clone
Multi target
Move
❓ Frequently Asked Questions
.insertAfter(target) inserts every element in the matched set immediately after the target element(s) 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.
Both insert content after target elements. With .after(), the anchor comes first: $('p').after('<hr>'). With .insertAfter(), the content comes first: $('<hr>').insertAfter('p'). Same DOM result, reversed syntax — like .append() vs .appendTo().
.insertAfter() inserts matched elements as the next sibling after the target. .insertBefore() inserts as the previous sibling before the target. Both use content-first syntax: $(content).insertAfter(target) vs $(content).insertBefore(target).
.insertAfter() inserts matched elements as siblings after the target — outside the target element. .appendTo() inserts matched elements as the last child inside the target. Use insertAfter for follow-up blocks; use appendTo to fill a container.
When inserting into a single target location, jQuery moves the element (not clones) and returns the inserted set. With multiple target elements, jQuery clones the inserted element for every target after the first — the original plus clones are returned.
No. Unlike .after(), which accepts a callback since 1.4, .insertAfter() only accepts a target argument. Build dynamic content first — for example $('<div>').text(msg).insertAfter('#field') — then call insertAfter on the prepared jQuery object.
Did you know?
jQuery’s four reversed-syntax pairs — .append() / .appendTo(), .prepend() / .prependTo(), .after() / .insertAfter(), and .before() / .insertBefore() — all follow the same rule: put the collection you already hold before the method, and pass the destination as the argument. jQuery 1.9 specifically fixed .insertAfter() and .appendTo() so single-target insertion returns the inserted set, making .end() chaining predictable in widget code.