From jQuery JavaScript Library
« Back to Traversing
andSelf( )
Add the previous selection to the current selection.
Useful for traversing elements, and then adding something that was matched before the last traversion.
Examples:| Name | Type |
Find all divs, and all the paragraphs inside of them, and give them both classnames. Notice the div doesn't have the yellow background color since it didn't use
andSelf().
$("div").find("p").andSelf().addClass("border");
$("div").find("p").addClass("background");
<!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").find("p").andSelf().addClass("border");
$("div").find("p").addClass("background");
});
</script>
<style>
p, div { margin:5px; padding:5px; }
.border { border: 2px solid red; }
.background { background:yellow; }
</style>
</head>
<body>
<div>
<p>First Paragraph</p>
<p>Second Paragraph</p>
</div>
</body>
</html>