1

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 extended to add an attribute in an array rather than a class.

Original Snippets that added value as classes:-

Gets date from div and add as class

jQuery(".publication-date").each(function(i) {
var yr = $(this).text().trim().split('/')[2];
jQuery(this).closest('.publication').addClass(yr);
});

Gets publication name from div and add as class

jQuery(".publication-name").each(function(i) {
var name = $(this).text().trim().replace(/[_\W]+/g, "-");

jQuery(this).closest('.publication').addClass(name); });

My Effort to combine and write into attribute of 'data-filter' where data-filter had any values separated with a comma ie. data-filter"2017, publicationname"

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

All input greatly appreciated.

Thanks in advance

Glennyboy

glennyboy
  • 153
  • 2
  • 15

1 Answers1

0

Assuming that, in your HTML, each of your .publication div has one .publicaton-date div and one publication-name div, 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.

mridula
  • 3,203
  • 3
  • 32
  • 55
  • Hi @miruda I'm looking to extend the above bit further by grouping the years (yr) automatically into a div with a class name of year + yr so for example a class of year2016 wrapped around every element that has a date of 2016. Then to show a div/span showing the actual year at the top of the grouped classes - so 2017, 2016 2015 etc. I'm guessing a combination of .wrapAll (for the classes) and .before / .prepend. (for the placeholder year value). Obviously only one set of year groups per year... – glennyboy Jun 21 '17 at 19:31
  • Its a little difficult for me to answer this without seeing some code. Can you show me what you are trying to achieve with the code you have tried for it? – mridula Jun 22 '17 at 04:20
  • Yes of course what I am trying to achive is to wrap a div around years and then also show what year it is. So right now I have:- `code` $(".grid").bind("transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){ $('.2017').wrapAll( '
    ' ); $('.year2017').before('

    2017

    '); }); // bind event listener $isotope.isotope( 'on', 'layoutComplete', onLayout ); `code`
    – glennyboy Jun 22 '17 at 09:58
  • I'm using jQuery Isotope and every time isotope runs I want to re-add and re-wrap Year Blocks with a year title. The full code is on the dev page here http://sanchez-stewart.webflow.io/publications (before

    tag and you'll see that my rudimentary code does wrap, but it's not dynamic and keeps reproducing. Actually it's a linked issue to https://stackoverflow.com/questions/44685217/call-a-function-init-and-when-isotope-finishes-and-then-reset-on-next-run

    – glennyboy Jun 22 '17 at 10:02