jQuery API

.after()

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

    contentAn element, HTML string, or jQuery object 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.

Consider the following HTML:

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

We can create content and insert it 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>

We can also select an element on the page and insert it after another:

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

If an element selected this way is inserted elsewhere, it will be moved after the target (not 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. We can further manipulate that set, even before inserting it 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>

As of jQuery 1.4, .after() allows us to pass a function that returns the elements to insert.

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

This inserts a <div> after each paragraph, containing the class name(s) of each paragraph in turn.

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.min.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.min.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.min.js"></script>
</head>
<body>
	<b>Hello</b><p>I would like to say: </p>
<script>$("p").after( $("b") );</script>

</body>
</html>

Demo:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .after() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • 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.
  • This statement seems true except for ie :s