tags:

views:

269

answers:

6

jQuery has an .after() method, and also an .insertAfter() method.

What's the difference between them? I think I can use .after() to insert elements after a selected element (or elements). Is that right? What's .insertAfter() for?

+7  A: 

They are mutual opposites.

'after' inserts the argument after the selector.

'insertAfter' inserts the selector after the argument.

(I just confused myself).

graphicdivine
+1  A: 
$("p").insertAfter("#foo");

==

$("#foo").after("p")
Svetlozar Angelov
+1  A: 

after( content ) Returns: jQuery

Insert content after each of the matched elements.

insertAfter( selector ) Returns: jQuery

Insert all of the matched elements after another, specified, set of elements.

just somebody
+1  A: 

Check the documentation:

$("#foo").after("p")

is the same as:

$("p").insertAfter("#foo");
kgiannakakis
+4  A: 

They are inverses of each other. As explained in the jQuery documentation:

This:

$("p").insertAfter("#foo");

Is the same as this:

$("#foo").after("p");

And lastly, insertAfter returns all inserted elements, whereas .after() has no return.

Chris Clark
+1  A: 
Dexter