|
35 | 35 | <desc>Add elements to the set of matched elements.</desc>
|
36 | 36 | <longdesc><p>Given a jQuery object that represents a set of DOM elements, the <code>.add()</code> method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to <code>.add()</code> can be pretty much anything that <code>$()</code> accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.</p>
|
37 | 37 | <p>The updated set of elements can be used in a following (chained) method, or assigned to a variable for later use. For example:</p>
|
38 |
| -<pre> |
| 38 | +<pre><code> |
39 | 39 | $("p").add("div").addClass("widget");
|
40 | 40 | var pdiv = $("p").add("div");
|
41 |
| -</pre> |
| 41 | +</code></pre> |
42 | 42 | <p>The following will <em>not</em> save the added elements, because the <code>.add()</code> method creates a new set and leaves the original set in pdiv unchanged:</p>
|
43 |
| -<pre> |
| 43 | +<pre><code> |
44 | 44 | var pdiv = $("p");
|
45 | 45 | pdiv.add("div"); // WRONG, pdiv will not change
|
46 |
| -</pre> |
| 46 | +</code></pre> |
47 | 47 | <p>Consider a page with a simple list and a paragraph following it:</p>
|
48 |
| -<pre><ul> |
| 48 | +<pre><code><ul> |
49 | 49 | <li>list item 1</li>
|
50 | 50 | <li>list item 2</li>
|
51 | 51 | <li>list item 3</li>
|
52 | 52 | </ul>
|
53 |
| -<p>a paragraph</p></pre> |
| 53 | +<p>a paragraph</p></code></pre> |
54 | 54 | <p>We can select the list items and then the paragraph by using either a selector or a reference to the DOM element itself as the <code>.add()</code> method's argument:</p>
|
55 |
| -<pre>$('li').add('p').css('background-color', 'red');</pre> |
| 55 | +<pre><code>$('li').add('p').css('background-color', 'red');</code></pre> |
56 | 56 | <p>Or:</p>
|
57 |
| -<pre>$('li').add(document.getElementsByTagName('p')[0]) |
58 |
| - .css('background-color', 'red');</pre> |
| 57 | +<pre><code>$('li').add(document.getElementsByTagName('p')[0]) |
| 58 | + .css('background-color', 'red');</code></pre> |
59 | 59 | <p>The result of this call is a red background behind all four elements.
|
60 | 60 | Using an HTML snippet as the <code>.add()</code> method's argument (as in the third version), we can create additional elements on the fly and add those elements to the matched set of elements. Let's say, for example, that we want to alter the background of the list items along with a newly created paragraph:</p>
|
61 |
| -<pre>$('li').add('<p id="new">new paragraph</p>') |
62 |
| - .css('background-color', 'red');</pre> |
| 61 | +<pre><code>$('li').add('<p id="new">new paragraph</p>') |
| 62 | + .css('background-color', 'red');</code></pre> |
63 | 63 | <p>Although the new paragraph has been created and its background color changed, it still does not appear on the page. To place it on the page, we could add one of the insertion methods to the chain.</p>
|
64 | 64 | <p>As of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation).</p>
|
65 | 65 | <p><strong>Note:</strong> To reverse the <code>.add()</code> you can use <a href="http://api.jquery.com/not"><code>.not( elements | selector )</code></a> to remove elements from the jQuery results, or <a href="http://api.jquery.com/end"><code>.end()</code></a> to return to the selection before you added.</p>
|
|
0 commit comments