How to get parent element by specified tag name using jquery?

I want to get a parent element with the specified tag name.

Code example:

<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 tagname. 
+42
jquery parent
Nov 15 '10 at 15:02
source share
3 answers

See .closest() :

Get the first ancestor element that matches the selector, starting from the current element and evolving through the DOM tree.

those.

 $('#myId').closest('table') 

( Demo )

+96
Nov 15 '10 at 15:02
source share
 $('#myId').closest("table"); 
+8
Nov 15 2018-10-15
source share

$('#myId').parents('table') also works

+1
Aug 02 '13 at 13:07 on
source share



All Articles