Jerry1
Jerry1

Reputation: 372

jquery DOM accessing parent element

I need to access to tr element in table.

I have 3 colums and in last column have checkbox. In event on change I have code:

alert( $(this).parent().parent().html() );

Which get all 3 td elements, but I need it with tr parent tag.

I tried:

alert( $(this).parent().parent().parent().html() );

alert( $(this).parent().parent().prev().html() );

And doesn't work.

HTML:

<table>
   <thead bgcolor="#668c41">
      <tr>
         <th>App</th>
         <th>Server</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody id="dialog-app">
      <tr>
         <td>appA</td>
         <td>serverA</td>
         <td><input checked="" type="checkbox"></td>
      </tr>
   </tbody>
</table>

Upvotes: 0

Views: 71

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

if you want to reach the closest enclosing tr , you can use closest()

$(this).closest('tr')[0].outerHTML // to get html including the tr

----> http://api.jquery.com/closest/

Upvotes: 1

Felix
Felix

Reputation: 38112

Try to use .closest() instead:

alert( $(this).closest('tr').html() );

If you want the outer HTML which is your closest tr element as well, then you can do:

alert($(this).closest('tr')[0].outerHTML)

Upvotes: 4

Related Questions