Open In App

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

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.




<!DOCTYpe html>
<html>
<head>
    <title>
        How to select direct parent element
        of an element in jQuery?
    </title>
 
    <script src=
    </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:




Article Tags :