Descendant Selector (“ancestor descendant”)


descendant selector

Description: Selects all elements that are descendants of a given ancestor.

  • version added: 1.0jQuery( "ancestor descendant" )

    ancestor: Any valid selector.

    descendant: A selector to filter the descendant elements.

A descendant of an element could be a child, grandchild, great-grandchild, and so on, of that element.

Example:

Mark all inputs that are descendants of a form with a dotted blue border. Give a yellow background to inputs that are descendants of a fieldset that is a descendant of a form.

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
<!DOCTYPE html>
<html>
<head>
<style>
body { font-size: 14px; }
form { border: 2px green solid; padding: 2px;
margin: 0; background: #efe; }
div { color: red; }
fieldset { margin: 1px; padding: 3px; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<form>
<div>Form is surrounded by the green border.</div>
<label>Child of form:</label>
<input name="name">
<fieldset>
<label>Grandchild of form, child of fieldset:</label>
<input name="newsletter">
</fieldset>
</form>
Sibling to form: <input name="none">
<script>
$( "form input" ).css( "border", "2px dotted blue" );
$( "form fieldset input" ).css( "backgroundColor", "yellow" );
</script>
</body>
</html>

Demo: