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 traverse a single level up the selected element and return that element.
Syntax:
$(selector).parent()
Example:
HTML
<!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:


