I want to get an Element's parent which has an specified tag name.
Sample code:
<table> <tr> <td> <input type='button' id='myId' /> </td> </tr> </table>
Now i want something like this:
$('#myId').specificParent('table'); //returns NEAREST parent of myId Element which table is it's tagname.
The parents() is an inbuilt method in jQuery which is used to find all the parent elements related to the selected element. This parents() method in jQuery traverse all the levels up the selected element and return that all elements.
click(function() { var parent = this. parentElement; var tagName = parent.
jQuery parent() Method The parent() method returns the direct parent element of the selected element.
parent() method returns the direct parent element of the selected one. This method only traverse a single level up the DOM tree. parents() method allows us to search through the ancestors of these elements in the DOM tree.
See .closest()
:
Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree.
I.e.,
$('#myId').closest('table')
(Demo)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With