You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<desc>An existing jQuery object to add to the set of matched elements.</desc>
24
+
</argument>
25
+
</signature>
26
+
<signature>
27
+
<added>1.4</added>
28
+
<argumentname="selector"type="Selector">
29
+
<desc>A string representing a selector expression to find additional elements to add to the set of matched elements.</desc>
30
+
</argument>
31
+
<argumentname="context"type="Element">
32
+
<desc>The point in the document at which the selector should begin matching; similar to the context argument of the <code>$(selector, context)</code> method.</desc>
33
+
</argument>
34
+
</signature>
35
+
<desc>Add elements to the set of matched elements.</desc>
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
+
<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>
39
+
$("p").add("div").addClass("widget");
40
+
var pdiv = $("p").add("div");
41
+
</pre>
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>
44
+
var pdiv = $("p");
45
+
pdiv.add("div"); // WRONG, pdiv will not change
46
+
</pre>
47
+
<p>Consider a page with a simple list and a paragraph following it:</p>
48
+
<pre><ul>
49
+
<li>list item 1</li>
50
+
<li>list item 2</li>
51
+
<li>list item 3</li>
52
+
</ul>
53
+
<p>a paragraph</p></pre>
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>
<p>The result of this call is a red background behind all four elements.
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>
<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
+
<p>As of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation).</p>
65
+
<p><strong>Note:</strong> To reverse the <code>.add()</code> you can use <ahref="http://api.jquery.com/not"><code>.not( elements | selector )</code></a> to remove elements from the jQuery results, or <ahref="http://api.jquery.com/end"><code>.end()</code></a> to return to the selection before you added.</p>
66
+
</longdesc>
67
+
<example>
68
+
<desc>Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.</desc>
69
+
<code><![CDATA[
70
+
71
+
$("div").css("border", "2px solid red")
72
+
.add("p")
73
+
.css("background", "yellow");
74
+
]]></code>
75
+
<css><![CDATA[
76
+
div { width:60px; height:60px; margin:10px; float:left; }
77
+
p { clear:left; font-weight:bold; font-size:16px;
78
+
color:blue; margin:0 10px; padding:2px; }
79
+
]]></css>
80
+
<html><![CDATA[<div></div>
81
+
82
+
<div></div>
83
+
<div></div>
84
+
<div></div>
85
+
<div></div>
86
+
<div></div>
87
+
88
+
<p>Added this... (notice no border)</p>]]></html>
89
+
</example>
90
+
<example>
91
+
<desc>Adds more elements, matched by the given expression, to the set of matched elements.</desc>
<desc>A function returning one or more space-separated class names to be added to the existing class name(s). Receives the index position of the element in the set and the existing class name(s) as arguments. Within the function, <code>this</code> refers to the current element in the set.</desc>
12
+
</argument>
13
+
</signature>
14
+
<desc>Adds the specified class(es) to each of the set of matched elements.</desc>
15
+
<longdesc><p>It's important to note that this method does not replace a class. It simply adds the class, appending it to any which may already be assigned to the elements.</p>
16
+
<p>More than one class may be added at a time, separated by a space, to the set of matched elements, like so:</p>
17
+
<pre>$("p").addClass("myClass yourClass");</pre>
18
+
<p>This method is often used with <code>.removeClass()</code> to switch elements' classes from one to another, like so:</p>
<desc>One or more additional DOM elements, arrays of elements, HTML strings, or jQuery objects to insert after each element in the set of matched elements.</desc>
9
+
</argument>
10
+
</signature>
11
+
<signature>
12
+
<added>1.4</added>
13
+
<argumentname="function(index)"type="Function">
14
+
<desc>A function that returns an HTML string, DOM element(s), or jQuery object to insert after each element in the set of matched elements. Receives the index position of the element in the set as an argument. Within the function, <code>this</code> refers to the current element in the set.</desc>
15
+
</argument>
16
+
</signature>
17
+
<desc>Insert content, specified by the parameter, after each element in the set of matched elements.</desc>
18
+
<longdesc><p>The <code>.after()</code> and <code><ahref='/insertAfter'>.insertAfter()</a></code> methods perform the same task. The major difference is in the syntax—specifically, in the placement of the content and target. With<code> .after()</code>, the selector expression preceding the method is the container after which the content is inserted. With <code>.insertAfter()</code>, on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted after the target container.</p>
19
+
20
+
<p>Using the following HTML:</p>
21
+
<pre><div class="container">
22
+
<h2>Greetings</h2>
23
+
<div class="inner">Hello</div>
24
+
<div class="inner">Goodbye</div>
25
+
</div></pre>
26
+
27
+
<p>Content can be created and then inserted after several elements at once:</p>
<p>Each inner <code><div></code> element gets this new content:</p>
32
+
33
+
<pre><div class="container">
34
+
<h2>Greetings</h2>
35
+
<div class="inner">Hello</div>
36
+
<p>Test</p>
37
+
<div class="inner">Goodbye</div>
38
+
<p>Test</p>
39
+
</div></pre>
40
+
41
+
<p>An element in the DOM can also be selected and inserted after another element:</p>
42
+
43
+
<pre>$('.container').after($('h2'));</pre>
44
+
45
+
<p>If an element selected this way is inserted elsewhere, it will be moved rather than cloned:</p>
46
+
47
+
<pre><div class="container">
48
+
<div class="inner">Hello</div>
49
+
<div class="inner">Goodbye</div>
50
+
</div>
51
+
<h2>Greetings</h2></pre>
52
+
<p>If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first.</p>
53
+
<h4id="disconnected-dom-nodes">Inserting Disconnected DOM nodes</h4>
54
+
<p>As of jQuery 1.4, <code>.before()</code> and <code>.after()</code> will also work on disconnected DOM nodes. For example, given the following code:</p>
<p>The result is a jQuery set containing a div and a paragraph, in that order. That set can be further manipulated, even before it is inserted in the document.</p>
<p>This example inserts a <code><div></code> after each paragraph, with each new <code><div></code> containing the class name(s) of its preceding paragraph.</p>
<p>Similar to other content-adding methods such as <code><ahref="http://api.jquery.com/prepend/">.prepend()</a></code> and <code><ahref="http://api.jquery.com/before/">.before()</a></code>, <code>.after()</code> also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.</p>
74
+
<p>For example, the following will insert two new <code><div></code>s and an existing <code><div></code> after the first paragraph:</p>
<p>Since <code>.after()</code> can accept any number of additional arguments, the same result can be achieved by passing in the three <code><div></code>s as three separate arguments, like so: <code>$('p').first().after($newdiv1, newdiv2, existingdiv1)</code>. The type and number of arguments will largely depend on the elements that are collected in the code.</p>
82
+
83
+
</longdesc>
84
+
<example>
85
+
<desc>Inserts some HTML after all paragraphs.</desc>
<desc>Register a handler to be called when Ajax requests complete. This is an <ahref='http://docs.jquery.com/Ajax_Events'>Ajax Event</a>.</desc>
9
+
<longdesc><p>Whenever an Ajax request completes, jQuery triggers the <code>ajaxComplete</code> event. Any and all handlers that have been registered with the <code>.ajaxComplete()</code> method are executed at this time.</p>
10
+
<p>To observe this method in action, we can set up a basic Ajax load request:</p>
<p>We can attach our event handler to any element:</p>
16
+
<pre>$('.log').ajaxComplete(function() {
17
+
$(this).text('Triggered ajaxComplete handler.');
18
+
});
19
+
</pre>
20
+
<p>Now, we can make an Ajax request using any jQuery method:</p>
21
+
<pre>$('.trigger').click(function() {
22
+
$('.result').load('ajax/test.html');
23
+
});</pre>
24
+
<p>When the user clicks the element with class <code>trigger</code> and the Ajax request completes, the log message is displayed.</p>
25
+
26
+
<p><strong>Note:</strong> Because <code>.ajaxComplete()</code> is implemented as a method of jQuery object instances, we can use the <code>this</code> keyword as we do here to refer to the selected elements within the callback function.</p>
27
+
28
+
<p>All <code>ajaxComplete</code> handlers are invoked, regardless of what Ajax request was completed. If we must differentiate between the requests, we can use the parameters passed to the handler. Each time an <code>ajaxComplete</code> handler is executed, it is passed the event object, the <code>XMLHttpRequest</code> object, and the settings object that was used in the creation of the request. For example, we can restrict our callback to only handling events dealing with a particular URL:</p>
29
+
30
+
<p><strong>Note:</strong> You can get the returned ajax contents by looking at <code>xhr.responseXML</code> or <code>xhr.responseHTML</code> for xml and html respectively.</p>
0 commit comments