Using inverted insertion methods

In Listing 5.13, we inserted content before an element, then appended that same element to another place in the document. Typically, when working with elements in jQuery, we can use chaining to perform multiple actions succinctly and efficiently. We weren't able to do that here, though, because this is the target of .insertBefore() and the subject of .appendTo(). The inverted insertion methods will help us get around this limitation.

Each of the insertion methods, such as .insertBefore() or .appendTo(), has a corresponding inverted method. The inverted methods perform exactly the same task as the standard ones, but the subject and target are reversed. For example:

 $('<p>Hello</p>').appendTo('#container'); ...

Get Learning jQuery 3 - Fifth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.