Add Class If Data-date Is Today Else Add Class To Previous Date
I'm using the code by Arg0n from add class if date is today which works great! I was wondered if it's possible to add only one class, so if attribute data-date equals today add cla
Solution 1:
Try this one
$(document).ready(function(){
today = newDate();
today.setDate(today.getDate()+1);
var currentDate = Date.parse((today).toLocaleDateString());
date = [];
$('.enewsarchive a').each(function(){
const [year, month, day] = $(this).attr('data-date').split("-");
temp = newDate($(this).attr('data-date').replace( /(\d{2})-(\d{2})-(\d{4})/, "$1/$3/$2" ));
if (currentDate >= temp) {
date.push(temp);
}
});
curr = newDate(Math.max.apply(null, date.map(function(e) {
returnnewDate(e);
})));
attr = curr.getFullYear()+'-'+(("0" + (curr.getMonth() + 1)).slice(-2))+'-'+(("0" + curr.getDate()).slice(-2))
$('.enewsarchivepost a[data-date="'+attr+'"]').addClass('active');
// Load iframe with archives
$('.enewsarchive a').click(function(e){
e.preventDefault();
$('#enews').attr('src',$(this).attr('href'));
$('.enewsarchive a.active').removeClass('active');
$(this).addClass('active');
});
});
Solution 2:
If I have understood your question correctly you need to add .active
class in only one a tag at a time?
If this is what you are looking for you can simply use below code.
$(".enewsarchive a.active").removeClass('active'); //remove active class from a tag before adding it
$(this).addClass('active'); // today
Is this what you are looking for? Or have I misunderstood the question?
Post a Comment for "Add Class If Data-date Is Today Else Add Class To Previous Date"