Note: To reverse the .add() you can use .not( selection before you added. Example 1:
elements | selector )
to remove elements from the jQuery results, or .end() to return to the
Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.
Javascript
$("div").css("border", "2px solid red") .add("p") .css("background", "yellow");
CSS div { width:60px; height:60px; margin:10px; float:left; } p { clear:left; font-weight:bold; font-size:16px; color:blue; margin:0 10px; padding:2px; }
HTML <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <p>Added this... (notice no border)</p>
Example 2:
Adds more elements, matched by the given expression, to the set of matched elements.
Javascript $("p").add("span").css("background", "yellow");
HTML <p>Hello</p><span>Hello Again</span>
Example 3:
Adds more elements, created on the fly, to the set of matched elements.
Javascript $("p").clone().add("<span>Again</span>").appendTo(document.body);
HTML <p>Hello</p>
Example 4:
Adds one or more Elements to the set of matched elements.
Javascript $("p").add(document.getElementById("a")).css("background", "yellow");
HTML <p>Hello</p><span id="a">Hello Again</span>
Example 5:
Demonstrates how to add (or push) elements to an existing collection