jQuery .after() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is .after()?

.after(content) inserts content immediately after each matched element as a sibling (since jQuery 1.0). It returns the original jQuery set for chaining.

Remember
$(target).after(content)

$(target).after("<b>Hi</b>")
$(target).after(domNode)
$(target).after($other)

// Sibling AFTER target — not inside it
// Same job as insertAfter, reversed syntax
NeedUse
Sibling after target$(target).after(content)
Content-first syntax$(content).insertAfter(target)
Sibling before target$(target).before(content)
Child inside target$(target).append(content)

How to Use .after()

Select the anchor element(s), then pass what should appear right after them.

  1. Select the target(s): $("p"), $(".item"), etc.
  2. Call .after(content) with HTML, a node, a jQuery object, or several arguments.
  3. Or pass a function (1.4+) that returns content for each matched element.
  4. Chain further methods on the same original selection if needed.
PieceRequired?Role
Matched setYesAnchor elements (must have a parent)
contentYes*HTML / node / array / jQuery object
function(index[, html])AltPer-element content (1.4 / 1.10+)
Return value—Original jQuery set (1.9+ always)

Pseudocode

Pseudocode
function after(elements, content):
    for each element in elements:
        if element has no parent: skip
        node = build(content)   // or clone for extra targets
        insert node as next sibling after element
    return original elements

Cheat Sheet

Keep these facts handy while inserting siblings.

TopicDetail
Syntax.after(content [, content…]) or .after(fn)
Since1.0 (callback 1.4; index+html callback 1.10)
WhereNext sibling (outside the element)
Move vs cloneMove for one target; clones for multiple
Disconnected nodeNo effect (jQuery 1.9+)
ReturnsOriginal set (chainable)

Live Preview

Pick a demo and insert content after the sample paragraph.

Browser demo of .after() — result shows a simplified DOM sketch.

Result
<p>I would like to say:</p>
<b>Hello</b>

jQuery Examples

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

Example 1 — Insert HTML After a Paragraph

Official style: pass an HTML string as the next sibling.

jQuery
$("p").after("<b>Hello</b>");
// <p>…</p><b>Hello</b>
Try It Yourself

How It Works

  1. Select every p in the document.
  2. Parse the HTML string into a <b> element.
  3. Insert that node right after each matched p.
  4. Outside the paragraph — sibling, not a child.

Example 2 — Move an Existing Element

Passing a jQuery selection moves (or clones) nodes already in the DOM.

jQuery
// <b>Hello</b>
// <p>I would like to say:</p>

$("p").after($("b"));
// <p>I would like to say:</p>
// <b>Hello</b>   ← moved after p
Try It Yourself

How It Works

  1. Select the existing <b> with $("b").
  2. One target — jQuery moves the node (no clone).
  3. New place is immediately after the paragraph.
  4. Many targets would clone for all but the last match.

Example 3 — Callback Per Element

Since 1.4, return different content for each matched element.

jQuery
$("p").after(function (index) {
  return "<span> after #" + index + "</span>";
});
Try It Yourself

How It Works

  1. Callback runs once per matched p.
  2. index is 0-based position in the set.
  3. this is the current DOM element.
  4. Returned HTML is inserted after that element.

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

Same result; content and target swap places in the call.

jQuery
$("p").after("<hr>");
$("<hr>").insertAfter("p");
// Both: <p>…</p><hr>
Try It Yourself

How It Works

  1. after: target first — $(target).after(content).
  2. insertAfter: content first — $(content).insertAfter(target).
  3. DOM ends up the same for one target.
  4. Pick the form that reads clearest in your chain.

Edge Cases & Pitfalls

Watch these when inserting siblings with .after().

xss

Untrusted HTML is dangerous

HTML strings can run scripts. Treat user input as unsafe unless sanitized.

clone

Multiple targets clone content

Existing nodes are cloned for every match except the last. Event handlers on moved nodes need care — use .clone(true) when you need data/events copied on purpose.

parent

No parent → no effect (1.9+)

Calling .after() on a disconnected node does nothing and still returns the original set.

inside

Not the same as .append()

Need content inside the element? Use append/prepend, not after/before.

Browser Support

.after() has shipped since jQuery 1.0 (callback form since 1.4). It works in all modern browsers with jQuery loaded. Behavior for disconnected nodes stabilized in jQuery 1.9.

✓ jQuery 1.0+

.after()

Supported everywhere jQuery DOM APIs run in the browser. Insert siblings after matched elements.

100% Browser support
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
.after() Excellent

Bottom line: Call $(target).after(content) for next-sibling insertion. Prefer insertAfter when content-first chaining reads better.

Performance Notes

ApproachCostBest for
.after(html)Parse + insertSimple sibling inserts
.after($nodes)Move / cloneReordering existing nodes
Many targets + clonesHigherBatch or build a fragment first

For lots of inserts, build a DocumentFragment (or one jQuery collection) and insert once when possible — fewer reflows than many tiny .after() calls.

Key Takeaways

  • Role: insert content as the next sibling after each match.
  • Syntax twin: insertAfter does the same job reversed.
  • Move/clone: one target moves; many targets clone.
  • Safety: never insert unsanitized user HTML.

One line: $(target).after(content) adds a next sibling — outside the element, not inside it.

Frequently Asked Questions

.after() inserts content immediately after each matched element as a next sibling (not inside it). It accepts HTML strings, DOM/text nodes, arrays, jQuery objects, multiple arguments, or a callback (since 1.4). Returns the original jQuery set for chaining.
.after() inserts as the next sibling; .before() inserts as the previous sibling. Neither adds children inside the matched element.
Same DOM result, reversed syntax: $(target).after(content) vs $(content).insertAfter(target) — like append vs appendTo.
.after() adds a sibling outside the element. .append() adds a child inside the element as the last child.
For a single target, an existing DOM node is moved. For multiple targets, jQuery clones for each target except the last (the original moves to the last target).
No. Like other HTML-accepting APIs, .after() can run script from untrusted strings. Never insert raw user input, URL params, or cookies without sanitizing.

Next: .before()

Insert content as the previous sibling before matched elements.

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

9 people found this page helpful