Get Data Attribute of First Child Element with jQuery
-
21-12-2019 - |
Question
I'm trying to get the data attributes of the first and last li in a ul. Something along the lines of...
var theTimeline = document.getElementById("the_timeline");
var timeline_items = $(theTimeline).filter('li');
var first_date = timline_items.lastChild.getAttribute("data-pubdate");
var last_date = timline_items.lastChild.getAttribute("data-pubdate");
Would any one be kind enough to give me a shove int the right direction?
Solution
First:
$("#the_timeline li").first().data("pubdate");
last:
$("#the_timeline li:last").data("pubdate");
OTHER TIPS
Since you've tagged it as jquery, you can do:
$('#the_timeline > li').first().data("pubdate");
And:
$('#the_timeline > li:last').data("pubdate");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow