jQuery .after() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

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

jQuery
$(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
CallWhat it does
.after()Learn jQuery .after(): insert HTML, DOM nodes, or jQuery objects as siblings after matched elements, callback form, after vs insertAfter, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Select the target(s): $("p"), $(".item"), etc.
Step 2Call .after(content) with HTML, a node, a jQuery object, or several arguments.
Step 3Or pass a function (1.4+) that returns content for each matched element.

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

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.

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.

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() Method

Insert content as the previous sibling before matched elements.

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

9 people found this page helpful