jQuery – :last-child Selector

The jQuery last-child selector selects all elements that are the last child of their parent. While :last matches only a single element, the :last-child selector can match more than one: one for each parent.

Source Code

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

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

Demo

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

Text 1

Text 2

Text 3

,

No comments yet.

Leave a Reply