16 of 119 menu

The after method

The after method adds text after the given element. There is also a insertAfter method that works in a similar way.

Syntax

Insert text after an element:

$(selector).after(text);

This is how the text that will be returned by the user function will be added after the selected elements. The function is called separately for each of the selected elements. The first parameter of this function gets the number in the set of each of the selected elements (in turn), the second - the current content of the element:

$(selector).after(function(number in set, current element content));

The text can also be not only regular text, but also a DOM element or a jQuery object. In this case, these elements will move from their position in the HTML code.

Example

Let's insert some text after the given paragraph:

<p id="test">text</p> $('#test').after('!!!');

HTML the code will look like this:

<p id="test">text</p>!!!

Example

Let's insert text with tags after the given paragraph:

<p id="test">text</p> $('#test').after('<p>!!!</p>');

HTML the code will look like this:

<p id="test">text</p><p>!!!</p>

Example

Let's move one paragraph under another (that is, cut the paragraph from the old place and put it in the new one):

<p id="p1">text1</p> <p id="p2">text2</p> $('#p1').after('#p2');

HTML the code will look like this:

<p id="p2">text2</p> <p id="p1">text1</p>

See also

enru