tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

.add( selector )

Returns: jQuery

Description: Add elements to the set of matched elements.

.add( selector )

version added: 1.0
selector

A string containing a selector expression to match additional elements against.

.add( elements )

version added: 1.0
elements

One or more elements to add to the set of matched elements.

.add( html )

version added: 1.0
html

An HTML fragment to add to the set of matched elements.

.add( selector, context )

version added: 1.4
selector

A string containing a selector expression to match additional elements against.

context

Add some elements rooted against the specified context.

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.

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

<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:

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

Or:

$('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:

$('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).

Examples

Example 1

Finds all divs and makes a border. Then adds all paragraphs to the jQuery object to set their backgrounds yellow.
    $("div").css("border", "2px solid red")
            .add("p")
            .css("background", "yellow");

The output of the code above will be:

Example 1 - Full source:

<!DOCTYPE html>

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script type="text/javascript" language="javascript">

  $(document).ready(function(){
    
    $("div").css("border", "2px solid red")
            .add("p")
            .css("background", "yellow");

  });
  </script>

  <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>

</head>
<body>
  <div></div>
  <div></div>

  <div></div>
  <div></div>

  <div></div>
  <div></div>

  <p>Added this... (notice no border)</p>
</body>
</html>

Example 2

Adds more elements, matched by the given expression, to the set of matched elements.
$("p").add("span").css("background", "yellow");
The output of the code above will be:

Example 2 - Full source:

<!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script type="text/javascript" language="javascript">

  $(document).ready(function(){
$("p").add("span").css("background", "yellow");

  });
  </script>

  
</head>
<body>
  <p>Hello</p><span>Hello Again</span>

</body>
</html>

Example 3

Adds more elements, created on the fly, to the set of matched elements
$("p").clone().add("<span>Again</span>").appendTo(document.body);
The output of the code above will be:

Example 3 - Full source:

<!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script type="text/javascript" language="javascript">

  $(document).ready(function(){
$("p").clone().add("<span>Again</span>").appendTo(document.body);

  });
  </script>
</head>
<body>
  <p>Hello</p>
</body>
</html>

Example 4

Adds one or more Elements to the set of matched elements.
$("p").add(document.getElementById("a")).css("background", "yellow");
The output of the code above will be:

Example 4 - Full source:

<!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" language="javascript">

  $(document).ready(function(){
$("p").add(document.getElementById("a")).css("background", "yellow");

  });
  </script>

  
</head>
<body>
  <p>Hello</p><span id="a">Hello Again</span>

</body>
</html>

Example 5

Demonstrates how to add (or push) elements to an existing collection
var collection = $("p");
// capture the new collection
collection = collection.add(document.getElementById("a"));
collection.css("background", "yellow");
The output of the code above will be:

Example 5 - Full source:

<!DOCTYPE html>

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" language="javascript">

  $(document).ready(function(){
    var collection = $("p");
// capture the new collection
collection = collection.add(document.getElementById("a"));
collection.css("background", "yellow");
  });
  </script>

  
</head>
<body>
  <p>Hello</p><span id="a">Hello Again</span>

</body>
</html>

Was this information helpful?
   

Comments