jQuery Methods: after()

By: Dusty Arlia
Published on Tuesday, December 3, 2013, 07:59 PM
Last Updated on Saturday, July 11, 2015 at 8:59 PM
Total Updates: 3

The jQuery after() method is invoked with only one argument. The after() method is invoked on the jQuery object it wants to insert something after. Here is the basic syntax:

$("[target_element]").after("[content]")

The target_element can be any CSS selector (e.g. HTML element, .class_name, #ex_id, etc.). If you are using a JavaScript variable, the parentheses are not necessary.

The content can be an HTML string (<br />) or a JavaScript variable (JavaScript variables do not require parentheses).

Here is an example:

<p id="test">Test Paragraph</p>

Now, if you wanted to insert <p>Paragraph 2</p> after <p>Test Paragraph</p>, you would do something like this:

$("#test").after("<p>Paragraph 2</p>")

And your result would look like this:

<p id="test">Test Paragraph</p>
<p>Paragraph 2</p>

Comments:

Ad: