jQuery – Child Selector (“parent > child”)

The jQuery selector selects all direct child elements specified by “child” of elements specified by “parent”.

Source Code

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<div id="div1">
	<h2>Item 1</h2>
	<h3>Item 2</h3>
</div>
<button>Click Here</button>

  
<script>
jQuery("button").click(function () {
	jQuery('#div1 > h2').css("border","2px solid #1E8CBE");
});
</script>
</body>
</html>

Demo

Following example finds all H2 items that are children of <div id=”div1″> and create a border around them.

Item 1

Item 2

,

No comments yet.

Leave a Reply