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

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