Oliver84
Oliver84

Reputation: 307

JQuery not adding class when used with a variable

I have the following function in my Meteor project:

function populate(){
  //console.log(Calendar.find().count())
  var dates = Calendar.find().fetch();
    for (var i = 0; i < dates.length; i++){
        var today = new Date(dates[i].date);
        //console.log('tbody.event-calendar tr td[date-month="' + (today.getMonth() + 1) + '"][date-day="' + today.getDate() + '"]')
        $('tbody.event-calendar tr td[date-month="' + (today.getMonth() + 1) + '"] [date-day="' + today.getDate() + '"]').addClass('event');
    }
}  

When I uncomment the console.logs I get the correct count and code such as: $("tbody.event-calendar tr td[date-month="8"][date-day="26"]").addClass("event") for the dates loops.

What's weird is that if I remove the variable in the jQuery and hard code a number (substitute today.getMonth() with 8 and today.getDate() with 1) the code works as expected.

What's causing my jQuery not to work with variables?

Upvotes: 0

Views: 41

Answers (1)

Viktor Maksimov
Viktor Maksimov

Reputation: 1465

Try without a space between date-month and date-day:

$('tbody.event-calendar tr td[date-month="' + (today.getMonth() + 1) + '"][date-day="' + today.getDate() + '"]').addClass('event');

Upvotes: 4

Related Questions