Open In App

jQuery prev() & prevAll() Method

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The prev() is an inbuilt function in jQuery which is used to return the previous sibling element of the selected element. Siblings are those having the same parent element in DOM Tree. Document Object Model(DOM) is a World Wide Web Consortium standard defined for accessing elements.

Syntax:

$(selector).prev()

Here selector is the selected element whose previous siblings get returned. 

Parameters: It does not accept any parameter. 

Return Value: It returns the previous sibling of the selected element.

jQuery code to show the working of prev() method:

Example 1: In this example we are using the above-explained method

html




<!DOCTYPE html>
<html>
 
<head>
    <style>
        .pre * {
            display: block;
            border: 2px solid lightgrey;
            color: black;
            padding: 5px;
            margin: 15px;
        }
    </style>
    <script src=
 
    </script>
    <script>
        $(document).ready(function () {
            $("h3").prev().css({
                "color": "black",
                "border": "2px solid green"
            });
        });
    </script>
</head>
 
<body class="pre">
    <div>
        This is parent element !
        <p>This is first paragraph </p>
        <span>First span box </span>
        <h2>heading 2 !</h2>
        <h3>heading 3 !</h3>
        <p>
              This is the second paragraph
            and next sibling to h3 !
        </p>
    </div>
</body>
 
</html>


Output: In the above code, the previous sibling element of “h3” get highlighted with green color. 

prevAll():

The prevAll() is an inbuilt method in jQuery that is used to return all the previous sibling elements of the selected element. 

Syntax:

$(selector).prevAll()

Here selector is the selected element whose previous siblings get returned. 

Parameters: It does not accept any parameter. 

Return Value: It returns all the previous sibling elements of the selected element.

jQuery code to show the working of prevAll() method:

Example 2: In this example, we are using the above-explained method.

html




<!DOCTYPE html>
<html>
 
<head>
    <style>
        .prevAll * {
            display: block;
            border: 2px solid lightgrey;
            color: black;
            padding: 5px;
            margin: 15px;
        }
    </style>
    <script src=
 
    </script>
    <script>
        $(document).ready(function () {
            $("h3").prevAll().css({
                "color": "black",
                "border": "2px solid green"
            });
        });
    </script>
</head>
 
<body class="prevAll">
    <div>
        This is parent element !
        <p>This is first paragraph </p>
        <span>first span box </span>
        <h2>heading 2 !</h2>
        <h3>heading 3 !</h3>
        <p>
              This is the second paragraph and next
            sibling to h3 !
        </p>
    </div>
</body>
 
</html>


Output: In the above code, all the previous sibling elements of “h3” get highlighted with green color. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads