jQuery – :first-child Selector

The jQuery first-child selector selects all elements that are the first child of their parent. While :first matches only a single element, the :first-child selector can match more than one: one for each parent. This is equivalent to :nth-child(1).

Source Code

<!DOCTYPE html>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
<div id="div1">
<h4>Text 1</h4>
<h4>Text 2</h4>
<h4>Text 3</h4>
<button>Click Here</button>

jQuery("button").click(function () {
	jQuery("#div1 h4:first-child").css({border:"3px #1E8CBE solid"});


Following example finds the first H4 element in each matched div and create a border around them.

Text 1

Text 2

Text 3


No comments yet.

Leave a Reply