Reputation: 3397
I have a table that looks essentially like this:
<table id="mytable">
<tbody>
<tr id="tempRowID">
<td class="delete"> <img src="myImage.png" /> </td>
</tr>
</tbody>
</table>
I need to put the table ID into a variable when the img is clicked. This function is working, but seems ugly with all the parent() jumps:
$(document).on('click', 'table td', function () {
var currentTable = $(this).parent().parent().parent().attr("id");
});
I have tried:
var currentTable = $(this).eq(3).attr("id");
var currentTable = $(this).find("table").attr("id");
Any advice?
Upvotes: 0
Views: 62
Reputation: 95023
Use .closest
to get the closest ancestor that is a table
.
var currentTable = $(this).closest("table").attr("id");
Upvotes: 2
Reputation: 55740
Use the .closest()
method
var currentTable = $(this).closest('table').attr("id");
Upvotes: 1
Reputation: 97672
How about using .closest()
var currentTable = $(this).closest('table').attr("id");
Upvotes: 1