jQuery – :nth-child() Selector

The jQuery nth-child selector selects all elements that are the nth-child of their parent.

Source Code

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

<div>
	<h4>Text 1</h4>
	<h4>Text 2</h4>
</div>
<div>
	<h4>Text 3</h4>
	<h4>Text 4</h4>
</div>
<button>Click Here</button>

  
<script>
jQuery("button").click(function () {
	jQuery("div h4:nth-child(2)").css({border:"3px #1E8CBE solid"});
});
</script>
</body>
</html>

Demo

Following example finds the second h4 in each matched div and create a border.

Text 1

Text 2

Text 3

Text 4

,

No comments yet.

Leave a Reply