jQuery .after() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM methods

What You’ll Learn

The .after() method inserts content immediately after each matched element as a sibling. This tutorial covers HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, and the callback form since 1.4, comparisons with .before(), .insertAfter(), and .append(), the official jQuery demos, and five practical try-it examples.

01

.after()

Next sibling

02

HTML

Strings

03

DOM

Move nodes

04

Callback

Since 1.4

05

vs .before()

Sibling pair

06

Since 1.0

Core API

Introduction

Form disclaimers, horizontal rules, helper text, and follow-up panels often need to appear right below an existing element — not inside it. When content should sit as the next sibling after a matched node, jQuery provides .after().

Available since jQuery 1.0, .after() is the mirror of .before(). Where .before() inserts content above the element, .after() inserts it below. You can pass HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, or a callback function (since 1.4).

.after() and .insertAfter() perform the same task with reversed syntax. Use .append() when content belongs inside the element. Browse the jQuery DOM hub for related manipulation methods.

Understanding the .after() Method

.after( content [, content ] ) inserts content immediately after each matched element in the DOM tree. The matched element itself is unchanged; new nodes appear as its following siblings. The return value is the same jQuery collection (for chaining).

When inserting an element already in the document after a single target, jQuery moves it rather than copying it. Since jQuery 1.4, the callback form .after( function( index ) ) runs once per matched element; return content to insert after that element.

💡
Beginner Tip

$(".inner").after(" Test ") adds “ Test ” after every .inner element — the official jQuery demo shows text appearing below “Hello” and “Goodbye”, not inside them.

📝 Syntax

jQuery .after() supports three forms:

Insert after — since 1.0

jQuery
.after( content [, content ] )
  • content — HTML string, DOM element, text node, array, or jQuery object.
  • Multiple arguments are inserted in order after each matched element.

Insert with callback — since 1.4

jQuery
.after( function( index [, html ] ) {
  // return content to insert; `this` is the current element
} )

Official after patterns

jQuery
$( "p" ).after( " Hello " );

⚡ Quick Reference

GoalCode
Insert HTML after element$("#section").after("<hr>")
Insert text node after$("p").after(document.createTextNode("Hi"))
Move existing element below$("#title").after($("#banner"))
Insert with callback$("p").after(function(){ return "<b>"+this.className+"</b>"; })
Insert above instead$("#section").before("<hr>")
Reversed syntax$("<hr>").insertAfter("#section")

📋 .after() vs .before() vs .insertAfter() vs .append()

Four related insertion APIs — pick the right position and syntax.

.after()
next sibling

Insert content immediately after matched elements — anchor first: $(target).after(content)

.before()
prev sibling

Insert content immediately before matched elements

.insertAfter()
same task

Insert after target — content first: $(content).insertAfter(target)

.append()
last child

Insert content inside matched elements as the last child

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 for HTML strings, text nodes, and existing elements.

Example 1 — Official Demo: Insert HTML After Paragraphs

Insert “ Hello ” immediately after every paragraph.

jQuery
$( "p" ).after( " Hello " );
Try It Yourself

How It Works

jQuery inserts the string as the next sibling after each matched element. The paragraph content is unchanged; the new text sits outside and below the closing tag. Avoid untrusted HTML strings to prevent XSS.

Example 2 — Official Demo: Insert a Text Node After

Insert plain text with document.createTextNode() instead of an HTML string.

jQuery
$( "p" ).after( document.createTextNode( "Hello" ) );
Try It Yourself

How It Works

Text nodes bypass HTML parsing. The text appears immediately after each paragraph as a sibling node, exactly like the HTML string version but without interpreting tags.

📈 Practical Patterns

Moving elements, callback insertion, and multiple-argument sibling insertion.

Example 3 — Official Demo: Insert a jQuery Object After

Insert an existing <b> element after paragraphs — jQuery moves it from its original location.

jQuery
$( "p" ).after( $( "b" ) );

// <b>Hello</b> moves to follow each <p>
// (cloned for all but last target if multiple matches)
Try It Yourself

How It Works

Inserting an existing DOM node after one target moves it to follow that element. With multiple targets, clones are created for all but the last match — same move-or-clone rules as .append() and .prepend().

Example 4 — Insert After With Callback Function

Generate unique content after each paragraph using the callback form (since 1.4) — official jQuery pattern.

jQuery
$( "p" ).after( function() {
  return "<b>" + this.className + "</b>";
});
Try It Yourself

How It Works

The callback receives the index (and optionally old HTML since 1.10). Return a string, DOM node, or jQuery object to insert after the current element. Inside the function, this refers to the DOM element being processed.

Example 5 — Multiple Arguments After First Paragraph

Pass several DOM elements and HTML strings in one call — official jQuery multi-argument pattern.

jQuery
var $newdiv1 = $( "<div class='note'>Note</div>" ),
    newdiv2 = document.createElement( "div" ),
    existingdiv1 = document.getElementById( "foo" );

newdiv2.textContent = "Created with createElement";

$( "p" ).first().after( $newdiv1, [ newdiv2, existingdiv1 ] );
Try It Yourself

How It Works

jQuery accepts arrays of nodes alongside individual arguments. Each item is inserted in order after the first paragraph. Existing elements move to the new location; newly created nodes are appended as siblings. Useful for inserting a disclaimer block, separator, and footer link in one call.

🚀 Common Use Cases

  • Form disclaimers — insert legal text or helper notes immediately after a form section.
  • Horizontal rules — add <hr> separators after content blocks.
  • Error messages — show validation feedback as a sibling below the invalid field wrapper.
  • Read-more links — append a “Continue reading” link after article excerpts.
  • Relocate elements — move an existing banner or widget to follow a heading.
  • Dynamic labels — use the callback form to insert badges showing each element’s class name.

🧠 How .after() Inserts Content

1

Match anchor elements

jQuery collection points at elements that will stay in place.

Select
2

Parse or accept content

HTML strings parsed; DOM nodes and jQuery objects inserted directly.

Content
3

Insert as next sibling

Content placed immediately after each matched element in the parent.

After
4

Return jQuery collection

Same anchor collection since jQuery 1.9 — ready for chaining.

📝 Notes

  • Available since jQuery 1.0 — callback form added in jQuery 1.4.
  • Inserts as the next sibling; use .before() for the previous sibling.
  • Matched elements are unchanged — content goes outside, not inside.
  • Inserting after a single target moves existing nodes; multiple targets clone except last.
  • Do not pass HTML strings from untrusted user input — XSS risk.
  • .append() inserts inside as last child; .after() inserts as sibling below.
  • Since jQuery 1.9, .after() always returns the original matched set.
  • Multiple arguments supported: .after(a, b, c).

Browser Support

.after() has been part of jQuery since 1.0+, with the callback form since 1.4+. It relies on standard DOM insertBefore operations on the parent node. 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 .after()

Universal sibling insertion API across jQuery 1.x, 2.x, 3.x, and 4.x. No browser-specific polyfills required — jQuery normalizes next-sibling insertion. Pair with .before() for above/below sibling patterns.

100% With jQuery loaded
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() Universal

Bottom line: Default choice for inserting content immediately after matched elements.

Conclusion

The jQuery .after() method inserts content as the next sibling after each matched element. Pass HTML strings, text nodes, DOM elements, jQuery objects, multiple arguments, or a callback function — exactly as the official jQuery demos demonstrate.

Use .before() when content belongs above the element, and .insertAfter() when content-first syntax reads better. For content inside a container, use .append() or .prepend() instead. Next up: the mirror sibling method .before().

💡 Best Practices

✅ Do

  • Use .after() for disclaimers, separators, and follow-up blocks below an element
  • Create elements with .text() for safe dynamic error messages
  • Use the callback form for per-element suffixes or class-name badges
  • Prefer .insertAfter() when building content before choosing the anchor
  • Check whether content belongs inside (.append) or outside (.after)

❌ Don’t

  • Use .after() when content should be a child inside the element
  • Pass untrusted HTML strings to .after() (XSS risk)
  • Confuse .after() with .append() (sibling vs child)
  • Assume inserting always clones — single target moves existing nodes
  • Call .after() on elements without a parent (no effect since 1.9)

Key Takeaways

Knowledge Unlocked

Six things to remember about .after()

The insert-below-sibling API — next sibling insertion made easy.

6
Core concepts
<> 02

HTML

Or DOM

Content
03

vs before

Prev sibling

Compare
fn 04

Callback

Since 1.4

Dynamic
05

vs append

Inside

Scope
1.9 06

Returns

Original set

Chain

❓ Frequently Asked Questions

.after() inserts the specified content immediately after each element in the matched set — as a sibling, not inside the element. It accepts HTML strings, DOM elements, text nodes, arrays, jQuery objects, multiple arguments, or a callback function (since 1.4). Returns the same jQuery collection for chaining. Available since jQuery 1.0.
.after() inserts content as the next sibling after each matched element. .before() inserts content as the previous sibling before each matched element. Both operate outside the matched element; neither adds children inside it.
Both insert content after target elements. With .after(), the matched element is the anchor: $('p').after('<hr>'). With .insertAfter(), the content comes first: $('<hr>').insertAfter('p'). Same DOM result, reversed syntax — like .append() vs .appendTo().
.after() inserts siblings outside and below the matched element. .append() inserts children inside the matched element as the last child. Use after for separators, disclaimers, or follow-up blocks; use append to fill a container.
When you insert an existing DOM element after a single target, jQuery moves it (not clones). If you call .after() on multiple matched elements, jQuery clones the inserted element for every target except the last — the original moves to the final target.
Since jQuery 1.4, yes. .after(function(index)) runs once per matched element. Return an HTML string, DOM node, or jQuery object to insert after that element. Inside the function, this refers to the current DOM element. Since jQuery 1.10, a two-argument form also receives the old HTML value.
Did you know?

jQuery’s .after() and .before() have been sibling-insertion pairs since version 1.0, while .insertAfter() and .insertBefore() offer the same behavior with content-first syntax — mirroring the .append() / .appendTo() pattern. Before jQuery 1.9, calling .after() on disconnected nodes could return an unpredictable collection; modern jQuery always returns the original matched set, making method chaining reliable again.

Next: jQuery .before() Method

Insert HTML, text nodes, or DOM elements as siblings immediately 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.

6 people found this page helpful