prev

UK [prɪv] US [prɪv]

[MEDICAL] Prevention

jquery prev() method syntax

Function: prev() obtains the immediately preceding sibling element of each element in the matching element set. Filtering through the selector is optional.

Syntax: .prev(selector)

Parameters:

Parameter Description
selector String value, containing the selector expression used to match elements.

Description: If given a jQuery object that represents a collection of DOM elements, the .prev() method allows us to search for these elements in the DOM tree. The previous sibling element and constructs a new jQuery object with the matching element. This method accepts an optional selector expression of the same type as the argument we passed into the $() function. If this selector is applied, elements will be filtered by testing whether they match the selector.

jquery prev() method example

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  <div><span>Hello</span></div>
  <p class="selected">Hello Again</p>
  <p>And Again</p>

<script>
  $("p").prev(".selected").css("background", "yellow");
</script>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance