Adding DOM Elements to the jQuery Object Set

var pars = $("p");   //contains all <p> elements var pAndDiv = $("div").add(pars);   //contains all <div> and <p> elements var pAndDivAndSpan = $("span").add("p").add("div");   //contains all <span> and <div> and <p> elements

A great feature of jQuery is how easy it is to add additional elements to the jQuery object set. The .add(selector) method adds elements that match the selector to the set represented by the jQuery object.

The .add(object) method accepts another jQuery object and adds the elements in that object to the current object’s set.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.