jQuery – Descendant Selector (“ancestor descendant”)

The jQuery Descendant Selector selects all elements that are descendants of a given ancestor.

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<div id="parent">
<h4>Child (select this)</h4>
<p><h4>Grand Child (select this)</h4></p>
</div>
<h4>Sibling (not selected)</h4>
<button>Click Here</button>  
<script>
jQuery("button").click(function () {
	jQuery("#parent h4").css("border","2px solid #1E8CBE");
});
</script>
</body>
</html>

Demo

Following example finds all H4 descendants of <div id=”parent”/> and create a border around them.

Child (select this)

Grand Child (select this)

Sibling (not selected)

,

Comments are closed.