53

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.

3 Answers 3

120

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)

0
10
$('#myId').closest("table");
0
3

$('#myId').parents('table') works as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.