Search code examples
jquerygetelementbyidgetattribute

Get Data Attribute of First Child Element with jQuery


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");