Skip to content Skip to sidebar Skip to footer

Combine 2 Functions To Write To An Attribute Array In Jquery Or Javascript

This is an extension of original question: jQuery Extract Year from Date and Add Class to Parent ... but I need to extend it so that the 2 existing functions are combined and then

Solution 1:

Assuming that, in your HTML, each of your .publicationdiv has one .publicaton-datediv and one publication-namediv, you can do something on the lines of:

$('.publication').each(function() {
    var yr = $(this).find('.publication-date').text().trim().split('/')[2];
    var name = $(this).find('.publication-name').text().trim().replace(/[_\W]+/g, "-");
    $(this).attr('data-filter', yr + ',' + name);
})

ie: Instead of looping through the .publication-date and .publication-name elements, loop through the .publication elements and find the date and year inside them.

Post a Comment for "Combine 2 Functions To Write To An Attribute Array In Jquery Or Javascript"