.add()


.add( selector )Returns: jQuery

Description: Add elements to the set of matched elements.

  • version added: 1.0.add( selector )

    • selector
      Type: Selector
      A string representing a selector expression to find additional elements to add to the set of matched elements.
  • version added: 1.0.add( elements )

    • elements
      Type: Elements
      One or more elements to add to the set of matched elements.
  • version added: 1.0.add( html )

    • html
      Type: htmlString
      An HTML fragment to add to the set of matched elements.
  • version added: 1.3.2.add( selection )

    • selection
      Type: jQuery
      An existing jQuery object to add to the set of matched elements.
  • version added: 1.4.add( selector, context )

    • selector
      Type: Selector
      A string representing a selector expression to find additional elements to add to the set of matched elements.
    • context
      Type: Element
      The point in the document at which the selector should begin matching; similar to the context argument of the $(selector, context) method.

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
$( "p" ).add( "div" ).addClass( "widget" );
var pdiv = $( "p" ).add( "div" );

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
var pdiv = $( "p" );
pdiv.add( "div" ); // WRONG, pdiv will not change

Consider a page with a simple list and a paragraph following it:

1
2
3
4
5
6
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
</ul>
<p>a paragraph</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 .add() method's argument:

1
$( "li" ).add( "p" ).css( "background-color", "red" );

Or:

1
2
$( "li" ).add( document.getElementsByTagName( "p" )[ 0 ] )
.css( "background-color", "red" );

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
$( "li" ).add( "<p id='new'>new paragraph</p>" )
.css( "background-color", "red" );

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.

Examples:

Example: 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>add demo</title>
<style>
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;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<p>Added this... (notice no border)</p>
<script>
$( "div" ).css( "border", "2px solid red" )
.add( "p" )
.css( "background", "yellow" );
</script>
</body>
</html>

Demo:

Example: 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>add demo</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Hello</p>
<span>Hello Again</span>
<script>
$( "p" ).add( "span" ).css( "background", "yellow" );
</script>
</body>
</html>

Demo:

Example: 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>add demo</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Hello</p>
<script>
$( "p" ).clone().add( "<span>Again</span>" ).appendTo( document.body );
</script>
</body>
</html>

Demo:

Example: 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>add demo</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Hello</p>
<span id="a">Hello Again</span>
<script>
$( "p" ).add( document.getElementById( "a" ) ).css( "background", "yellow" );
</script>
</body>
</html>

Demo:

Example: 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>add demo</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Hello</p>
<span id="a">Hello Again</span>
<script>
var collection = $( "p" );
// Capture the new collection
collection = collection.add( document.getElementById( "a" ) );
collection.css( "background", "yellow" );
</script>
</body>
</html>

Demo: