Reputation: 6763
How can i get the data attribute of the last table row?
<tbody id="tableNotifications">
<tr class="notificationsRowEntry success" data-notification-timestamp="1384105387"></tr>
<tr class="notificationsRowEntry success" data-notification-timestamp="1384105367"></tr>
<tr class="notificationsRowEntry success" data-notification-timestamp="1384105357"></tr>
<tr class="notificationsRowEntry success" data-notification-timestamp="1384105323"></tr>
</tbody>
The problem is that i can't get the data attribute but only the content of the last table row using:
$data = $("#tableNotifications").last().data("notification-timestamp");
Maybe i'm doing a stupid mistake but it's driving me crazy.
Thank you.
P.S. I know that i could add some unique ids and then fetch the data with
$data = $(uniqueid).data("notification-timestamp");
but i would like to avoid that.
Upvotes: 1
Views: 2795
Reputation: 113385
Use attr()
function:
var data = $("#tableNotifications tr:last").attr("data-notification-timestamp");
The selector #tableNotifications tr:last
selects the last
tr
child of the element with id tableNotifications
.
Also this will work too:
var data = $("#tableNotifications tr").last().attr("data-notification-timestamp");
Just to clarify:
If you just want to get the attribute value from HTML use .attr("data-attr")
. For more information see the difference between .data()
and .attr()
.
Upvotes: 3
Reputation: 16184
That's finding the last element with the id of 'tableNotifications'. Simply append tr
to your selector to get the last row:
$data = $("#tableNotifications tr").last().data("notification-timestamp");
Upvotes: 3
Reputation: 219936
You want to get the value of the data attribute of the tr
, not the tbody
itself:
$("#tableNotifications > tr:last-child").data("notification-timestamp");
Upvotes: 3