I'm trying to use data- attributes in an HTML table to store data about an item that will later be used in an ajax call, but I'm having trouble with my jQuery selection when I try to get the data back out.
This works:
var listItemId = $(this).parent().parent().attr('data-id');
However, the .parent().parent()
selector is too fragile--it'll break if I ever change my HTML structure.
This is what I'd like to do, but it is returning undefined:
var listItemId = $(this).parent('.list-item').attr('data-id');
My HTML looks like this:
<tr class="list-item" data-id="1">
<td>
<input value="Some text entered by the user" type="text" >
</td>
</tr>
What am I missing?