I want javascript to add a class to a html element, depending of the current day and month. To do this, I would like to compare the class of the elements (“09-20”, “09-21”, …) with the day and month of the current date.
Unfortunately my script doesn’t work. I’m an absolute beginner in javascript, so sorry for my rookie mistakes.
<div class="grid-item" data-date="09-20"></div>
<div class="grid-item" data-date="09-21"></div>
<div class="grid-item" data-date="09-22"></div>
<div class="grid-item" data-date="09-23"></div>
<div class="grid-item" data-date="09-24"></div>
<div class="grid-item" data-date="09-25"></div>
<div class="grid-item" data-date="09-26"></div>
.grid-item {
height: 130px;
width: 130px;
float: left;
background: red;
margin: 10px;
}
.today {
background: yellow;
border: red 1px solid;
}
$(function() {
var a = new Date();
$(".grid-item").each(function() {
var specifiedDate = $(this).data('date');
if (a.getMonth() + "-" + a.getDay() == specifiedDate) {
$(this).addClass('today');
}
});
});