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?
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?
They are mutual opposites.
'after' inserts the argument after the selector.
'insertAfter' inserts the selector after the argument.
(I just confused myself).
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.
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.