jQuery & jQuery UI Documentation

jQuery & jQuery UI

Next Adjacent Selector ("prev + next")

next adjacent selector

version added: 1.0jQuery('prev + next')

  • prev
    Any valid selector.
    next
    A selector to match the element that is next to the first selector.

Description: Selects all next elements matching "next" that are immediately preceded by a sibling "prev".

One important point to consider with both the next adjacent sibling selector (prev + next) and the general sibling selector (prev ~ siblings) is that the elements on either side of the combinator must share the same parent.

Example:

Finds all inputs that are next to a label.

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

    <label>Name:</label>
    <input name="name" />
    <fieldset>
      <label>Newsletter:</label>

      <input name="newsletter" />
    </fieldset>
  </form>
  <input name="none" />
<script>$("label + input").css("color", "blue").val("Labeled!")</script>

</body>
</html>