Open In App

How to select direct parent element of an element in jQuery ?

Last Updated : 17 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will select the direct parent element of an element using jQuery. To select the direct parent element, the parent() method is used. This method finds the parent element related to the selected element. This parent() method traverses a single level up the selected element and returns that element.

Syntax:

$(selector).parent()

Example:  Here is the implementation of the above method.

HTML
<!DOCTYpe html>
<html>
<head>
    <title>
        How to select direct parent element
        of an element in jQuery?
    </title>

    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>

    <script>
        $(document).ready(function () {
            $("li").parent().css({
                color: "white",
                background: "green"
            });
        });
    </script>
</head>

<body>
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>

    <h3>
        How to select direct parent element
        of an element in jQuery?
    </h3>

    <p>GeeksforGeeks Subjects List-</p>

    <ul>
        <li>HTML</li>
        <li>CSS</li>
        <li>JavaScript</li>
        <li>PHP</li>
    </ul>
</body>
</html>

Output:


Next Article

Similar Reads