jQuery API

.after()

.after( content, [content] ) Returns: jQuery

Description: Insert content, specified by the parameter, after each element in the set of matched elements.

  • version added: 1.0.after( content, [content] )

    contentHTML string, DOM element, or jQuery object to insert after each element in the set of matched elements.

    contentOne or more additional DOM elements, arrays of elements, HTML strings, or jQuery objects to insert after each element in the set of matched elements.

  • version added: 1.4.after( function(index) )

    function(index)A function that returns an HTML string to insert after each element in the set of matched elements.

The .after() and .insertAfter() methods perform the same task. The major difference is in the syntax—specifically, in the placement of the content and target. With .after(), the selector expression preceding the method is the container after which the content is inserted. With .insertAfter(), on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted after the target container.

Using the following HTML:

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>

Content can be created and then inserted after several elements at once:

$('.inner').after('<p>Test</p>');

Each inner <div> element gets this new content:

<div class="container">
  <h2>Greetings</h2>
  <div class="inner">Hello</div>
  <p>Test</p>
  <div class="inner">Goodbye</div>
  <p>Test</p>
</div>

An element in the DOM can also be selected and inserted after another element:

$('.container').after($('h2'));

If an element selected this way is inserted elsewhere, it will be moved rather than cloned:

<div class="container">
  <div class="inner">Hello</div>
  <div class="inner">Goodbye</div>
</div>
<h2>Greetings</h2>

If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first.

Inserting Disconnected DOM nodes

As of jQuery 1.4, .before() and .after() will also work on disconnected DOM nodes. For example, given the following code:

$('<div/>').after('<p></p>');

The result is a jQuery set containing a div and a paragraph, in that order. That set can be further manipulated, even before it is inserted in the document.

$('<div/>').after('<p></p>').addClass('foo')
  .filter('p').attr('id', 'bar').html('hello')
.end()
.appendTo('body');

This results in the following elements inserted just before the closing </body> tag:

<div class="foo"></div>
<p class="foo" id="bar">hello</p>

Passing a Function

As of jQuery 1.4, .after() supports passing a function that returns the elements to insert.

$('p').after(function() {
  return '<div>' + this.className + '</div>';
});

This example inserts a <div> after each paragraph, with each new <div> containing the class name(s) of its preceding paragraph.

Additional Arguments

Similar to other content-adding methods such as .prepend() and .before(), .after() also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.

For example, the following will insert two new <div>s and an existing <div> after the first paragraph:

var $newdiv1 = $('<div id="object1"/>'),
    newdiv2 = document.createElement('div'),
    existingdiv1 = document.getElementById('foo');

$('p').first().after($newdiv1, [newdiv2, existingdiv1]);

Since .after() can accept any number of additional arguments, the same result can be achieved by passing in the three <div>s as three separate arguments, like so: $('p').first().after($newdiv1, newdiv2, existingdiv1). The type and number of arguments will largely depend on the elements are collected in the code.

Examples:

Example: Inserts some HTML after all paragraphs.

<!DOCTYPE html>
<html>
<head>
  <style>p { background:yellow; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>I would like to say: </p>
<script>$("p").after("<b>Hello</b>");</script>

</body>
</html>

Demo:

Example: Inserts a DOM element after all paragraphs.

<!DOCTYPE html>
<html>
<head>
  <style>p { background:yellow; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>I would like to say: </p>
<script>$("p").after( document.createTextNode("Hello") );</script>

</body>
</html>

Demo:

Example: Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.

<!DOCTYPE html>
<html>
<head>
  <style>p { background:yellow; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <b>Hello</b><p>I would like to say: </p>
<script>$("p").after( $("b") );</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .after() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .after()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Chinmaya87
    Is there any way to moveup and movedown the whole table itself in a series of table structures?
  • Michael Jones
    The after() method seems to fail when there's a closing tag in the beginning, consider:
    html:
    <div id="container">I'm a container</div>

    jquery:
    $("#container").before("<div><div>Hi");
    $("#container").after("
    </div>There</div>");

    it seems that the open divs in the 'before()' will close themselves. Is there a way to stop that from happening?
  • Yeah, you can't do it like that. Keep in mind that what you're doing is inserting DOM nodes, not just strings. Therefore, you need to insert well-formed (x)html.

    It looks like what you're trying to do would be better served by the .wrap() method.
  • ericbojo
    Is there any function that does something like ifAfter('selector'). I want to test if a given element is after another element in the DOM
  • Bob
    Well, you could use the nextSibling property for an element and apply jQuery conditionals to it. For example, to test if a span element is after a given element, you could do:

    $(spanElement.nextSibling).is('span');

    or

    $($('#spanSibling').nextSibling).is('span');
  • Bob
    Sorry, that last one should be:

    $($('#spanSibling')[0].nextSibling).is('span');

    There should be an option to edit a post you just made...
  • milesslow
    These are two possible solutions, both using the .prev() function:

    If you want to know if there is an element matching 'selector' ANYWHERE before the current element, you can use .prev('selector'). You would then check whether or not there are any matched elements in the return value (.length == 0).

    If you want to know if the current element is IMMEDIATELY AFTER an element matching 'selector', then you can use .prev().filter('selector'), and again check whether or not there are any matched elements in the return value.
  • milesslow
    At the very least, the documentation is unclear. I, too, expected it to behave that way on both connected and disconnected nodes. Also, I would expect chained calls to .after() to work the way they currently do on disconnected nodes. I'll file a bug and see if we can't get that fixed.
  • chrisovenden
    Can you be a bit clearer about what .after() returns? In jQ 1.4 the jQ object returned appears to contain just the element you started with, not, as the documentation states, a jQ object containing both the initial object and the element created.
  • The documentation is correct, but note that it is only referring to situations in which both are disconnected DOM nodes--elements created on the fly that haven't been inserted into the DOM yet.
  • The documentation is accurate here, but I think it deserves to be specifically called out. Calling n1.after(n2) on two disconnected nodes will *return* a jQuery object containing two elements (n1 and n2). It will *not* modify n1 to include n2 (it does not append n2 to n1).

    Consider:

    var el1 = $('<div>'),
    el2 = $('');

    // Outputs 2, the length of the value returned by el1.after(el2), which contains both.
    console.log('Length 1: %d', el1.after(el2).length);

    // Outputs 1, the length of el1 which has not been modified.
    console.log('Length 2: %d', el1.length);
    </div>
  • The JS got filtered in a weird way, but pretend both el1 and el2 were initially defined as proper DIVs.
  • Anonomom
    Interesting... The docs could be more explicit about that.

    What's the reason for this irregularity? Compatibility?