From jQuery JavaScript Library
« Back to Traversing
add( expr )
Adds more elements, matched by the given expression, to the set of matched elements.
Arguments:| expr | String, DOMElement, Array<DOMElement> | |
|---|
| An expression whose matched elements are added for String, a string of HTML to create on the fly for DOMElement or one or more Elements to add if an Array. |
Examples:| Name | Type |
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");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../jquery-latest.js"></script>
<script>
$(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>
Adds more elements, matched by the given expression, to the set of matched elements.
$("p").add("span").css("background", "yellow");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("p").add("span").css("background", "yellow");
});
</script>
</head>
<body>
<p>Hello</p><span>Hello Again</span>
</body>
</html>
Adds more elements, created on the fly, to the set of matched elements.
$("p").clone().add("<span>Again</span>").appendTo(document.body);
Adds one or more Elements to the set of matched elements.
$("p").add(document.getElementById("a")).css("background", "yellow");
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../jquery-latest.js"></script>
<script>
$(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>