Description: Create a new jQuery object with elements added to the set of matched elements.
Given a jQuery object that represents a set of DOM elements, the .add() method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to .add() can be pretty much anything that $() accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.
Do not assume that this method appends the elements to the existing collection in the order they are passed to the .add() method. When all elements are members of the same document, the resulting collection from .add() will be sorted in document order; that is, in order of each element's appearance in the document. If the collection consists of elements from different documents or ones not in any document, the sort order is undefined. To create a jQuery object with elements in a well-defined order and without sorting overhead, use the $(array_of_DOM_elements) signature.
The updated set of elements can be used in a following (chained) method, or assigned to a variable for later use. For example:
|
1
2
|
|
The following will not save the added elements, because the .add() method creates a new set and leaves the original set in pdiv unchanged:
|
1
2
|
|
Consider a page with a simple list and a paragraph following it:
|
1
2
3
4
5
6
|
|
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 .add() method's argument:
|
1
|
|
Or:
|
1
2
|
|
The result of this call is a red background behind all four elements.
Using an HTML snippet as the .add() 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:
|
1
2
|
|
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.
As of jQuery 1.4 the results from .add() will always be returned in document order (rather than a simple concatenation).
Note: To reverse the .add() you can use .not( elements | selector ) to remove elements from the jQuery results, or .end() to return to the selection before you added.
Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
|
Adds more elements, matched by the given expression, to the set of matched elements.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
|
Adds more elements, created on the fly, to the set of matched elements.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
|
Adds one or more Elements to the set of matched elements.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
|
Demonstrates how to add (or push) elements to an existing collection
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
|
Description: Add the previous set of elements on the stack to the current set, optionally filtered by a selector.
As described in the discussion for .end(), jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, .addBack() can help.
Consider a page with a simple list on it:
|
1
2
3
4
5
6
7
|
|
The result of the following code is a red background behind items 3, 4 and 5:
|
1
2
|
|
First, the initial selector locates item 3, initializing the stack with the set containing just this item. The call to .nextAll() then pushes the set of items 4 and 5 onto the stack. Finally, the .addBack() invocation merges these two sets together, creating a jQuery object that points to all three items in document order: {[<li.third-item>,<li>,<li> ]}.
The .addBack() method causes the previous set of DOM elements in the traversal stack to be added to the current set. In the first example, the top stack contains the set resulting from .find("p"). In the second example, .addBack() adds the previous set of elements on the stack — in this case $("div.after-addback") — to the current set, selecting both the div and its enclosed paragraphs.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
|
Description: Adds the specified class(es) to each element in the set of matched elements.
this refers to the current element in the set.this refers to the current element in the set.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.
Before jQuery version 1.12/2.2, the .addClass() method manipulated the className property of the selected elements, not the class attribute. Once the property was changed, it was the browser that updated the attribute accordingly. An implication of this behavior was that this method only worked for documents with HTML DOM semantics (e.g., not pure XML documents).
As of jQuery 1.12/2.2, this behavior is changed to improve the support for XML documents, including SVG. Starting from this version, the class attribute is used instead. So, .addClass() can be used on XML or SVG documents.
More than one class may be added at a time, separated by a space, to the set of matched elements, like so:
|
1
|
|
This method is often used with .removeClass() to switch elements' classes from one to another, like so:
|
1
|
|
Here, the myClass and noClass classes are removed from all paragraphs, while yourClass is added.
As of jQuery 1.4, the .addClass() method's argument can receive a function.
|
1
2
3
|
|
Given an unordered list with two <li> elements, this example adds the class "item-0" to the first <li> and "item-1" to the second.
Add the class "selected" to the matched elements.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
|
Add the classes "selected" and "highlight" to the matched elements.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
|
Add the classes "selected" and "highlight" to the matched elements (3.3+ syntax).
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
|
Pass in a function to .addClass() to add the "green" class to a div that already has a "red" class.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
|
Description: Insert content, specified by the parameter, after each element in the set of matched elements.
this refers to the current element in the set.this refers to the current element in the set.The .after() and .insertAfter() methods perform the same task. The major difference is in the syntax—specifically, in the placement of the content and target. With .after(), the content to be inserted comes from the method's argument: $(target).after(contentToBeInserted). With .insertAfter(), on the other hand, the content precedes the method and is inserted after the target, which in turn is passed as the .insertAfter() method's argument: $(contentToBeInserted).insertAfter(target).
Using the following HTML:
|
1
2
3
4
5
|
|
Content can be created and then inserted after several elements at once:
|
1
|
|
Each inner <div> element gets this new content:
|
1
2
3
4
5
6
7
|
|
An element in the DOM can also be selected and inserted after another element:
|
1
|
|
If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved rather than cloned:
|
1
2
3
4
5
|
|
Important: If there is more than one target element, however, cloned copies of the inserted element will be created for each target except for the last one.
As of jQuery 1.4, .after() supports passing a function that returns the elements to insert.
|
1
2
3
|
|
This example inserts a <div> after each paragraph, with each new <div> containing the class name(s) of its preceding paragraph.
Similar to other content-adding methods such as .prepend() and .before(), .after() also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.
For example, the following will insert two new <div>s and an existing <div> after the first paragraph:
|
1
2
3
4
5
|
|
Since .after() can accept any number of additional arguments, the same result can be achieved by passing in the three <div>s as three separate arguments, like so: $( "p" ).first().after( $newdiv1, newdiv2, existingdiv1 ). The type and number of arguments will largely depend on the elements that are collected in the code.
.after() would attempt to add or change nodes in the current jQuery set if the first node in the set was not connected to a document, and in those cases return a new jQuery set rather than the original set. The method might or might not have returned a new result depending on the number or connectedness of its arguments! As of jQuery 1.9, .after(), .before(), and .replaceWith() always return the original unmodified set. Attempting to use these methods on a node without a parent has no effect—that is, neither the set nor the nodes it contains are changed.
<img onload="">). Do not use these methods to insert strings obtained from untrusted sources such as URL query parameters, cookies, or form inputs. Doing so can introduce cross-site-scripting (XSS) vulnerabilities. Remove or escape any user input before adding content to the document.
Inserts some HTML after all paragraphs.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
|
Inserts a DOM element after all paragraphs.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
|
Inserts a jQuery object (similar to an Array of DOM Elements) after all paragraphs.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
|
Description: Register a handler to be called when Ajax requests complete. This is an AjaxEvent.
"ajaxComplete".This page describes the ajaxComplete event. For the deprecated .ajaxComplete() method, see .ajaxComplete().
Whenever an Ajax request completes, jQuery triggers the ajaxComplete event. Any and all registered ajaxComplete handlers are executed at this time.
To observe this method in action, set up a basic Ajax load request:
|
1
2
3
|
|
Attach the event handler to the document:
|
1
2
3
|
|
Now, make an Ajax request using any jQuery method:
|
1
2
3
|
|
When the user clicks the element with class trigger and the Ajax request completes, the log message is displayed.
All ajaxComplete handlers are invoked, regardless of what Ajax request was completed. If you must differentiate between the requests, use the parameters passed to the handler. Each time an ajaxComplete handler is executed, it is passed the event object, the XMLHttpRequest object, and the settings object that was used in the creation of the request. For example, you can restrict the callback to only handling events dealing with a particular URL:
|
1
2
3
4
5
6
|
|
Note: You can get the returned Ajax contents by looking at xhr.responseText.
.on( "ajaxComplete", ... ), must be attached to document.
$.ajax() or $.ajaxSetup() is called with the global option set to false, the ajaxComplete event will not fire.
Show a message when an Ajax request completes.
|
1
2
3
|
|
Description: Register a handler to be called when Ajax requests complete. This is an AjaxEvent.
This API is deprecated. Use .on( "ajaxComplete", handler ) instead.
Description: Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.
"ajaxError".This page describes the ajaxError event. For the deprecated .ajaxError() method, see .ajaxError().
Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all registered ajaxError handlers are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.
To observe this method in action, set up a basic Ajax load request.
|
1
2
3
|
|
Attach the event handler to the document:
|
1
2
3
|
|
Now, make an Ajax request using any jQuery method:
|
1
2
3
|
|
When the user clicks the button and the Ajax request fails, because the requested file is missing, the log message is displayed.
All ajaxError handlers are invoked, regardless of what Ajax request was completed. To differentiate between the requests, use the parameters passed to the handler. Each time an ajaxError handler is executed, it is passed the event object, the jqXHR object (prior to jQuery 1.5, the XHR object), and the settings object that was used in the creation of the request. When an HTTP error occurs, the fourth argument (thrownError) receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." For example, to restrict the error callback to only handling events dealing with a particular URL:
|
1
2
3
4
5
|
|
.on( "ajaxError", ... ), must be attached to document.
$.ajax() or $.ajaxSetup() is called with the global option set to false, the ajaxError event will not fire.
Show a message when an Ajax request fails.
|
1
2
3
|
|
Description: Register a handler to be called when Ajax requests complete with an error. This is an Ajax Event.
This API is deprecated. Use .on( "ajaxError", handler ) instead.
Description: Attach a function to be executed before an Ajax request is sent. This is an Ajax Event.
"ajaxSend".This page describes the ajaxSend event. For the deprecated .ajaxSend() method, see .ajaxSend().
Whenever an Ajax request is about to be sent, jQuery triggers the ajaxSend event. Any and all registerd ajaxSend handlers are executed at this time.
To observe this method in action, set up a basic Ajax load request:
|
1
2
3
|
|
Attach the event handler to the document:
|
1
2
3
|
|
Now, make an Ajax request using any jQuery method:
|
1
2
3
|
|
When the user clicks the element with class trigger and the Ajax request is about to begin, the log message is displayed.
All ajaxSend handlers are invoked, regardless of what Ajax request is to be sent. If you must differentiate between the requests, use the parameters passed to the handler. Each time an ajaxSend handler is executed, it is passed the event object, the jqXHR object (in version 1.4, XMLHttpRequestobject), and the settings object that was used in the creation of the Ajax request. For example, you can restrict the callback to only handling events dealing with a particular URL:
|
1
2
3
4
5
|
|
.on( "ajaxSend", ... ), must be attached to document.
$.ajax() or $.ajaxSetup() is called with the global option set to false, the ajaxSend event will not fire.
Show a message before an Ajax request is sent.
|
1
2
3
|
|
Description: Attach a function to be executed before an Ajax request is sent. This is an Ajax Event.
This API is deprecated. Use .on( "ajaxSend", handler ) instead.