jQuery – :only-child Selector

The jQuery only-child selector selects all elements that are the only child of their parent. If the parent has other child elements, nothing is matched.

Source Code

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

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

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


Following example finds each h4 element that is the only child of div and create a border.

Text 1

Text 3

Text 4


No comments yet.

Leave a Reply