rahim asgari
rahim asgari

Reputation: 12437

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

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.

Upvotes: 53

Views: 64965

Answers (3)

donah
donah

Reputation: 31

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

Upvotes: 3

jensgram
jensgram

Reputation: 31498

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)

Upvotes: 120

NimChimpsky
NimChimpsky

Reputation: 47290

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

Upvotes: 10

Related Questions