jQuery – Next Adjacent Selector (“prev + next”)

The jQuery next adjacent selector selects all next elements matching “next” that are immediately preceded by a sibling “prev”.

Source Code

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

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

Demo

Following example finds all h5 that are next to a h4.

Text 1

Text 2

Text 3

Text 4

,

No comments yet.

Leave a Reply