user1172465
user1172465

Reputation: 63

jQuery - addClass if day is today

Simple question; I want to highlight some text if it is monday, some other when it's tuesday etc.

$('p.day:eq("' + new Date().getDay() + '")').addClass('today');

I feel like I'm so close.

jsFiddle.

Upvotes: 2

Views: 968

Answers (3)

ahren
ahren

Reputation: 16961

$('p.day:eq(' + new Date().getDay() + ')').addClass('today');

You need to take out the quotes in the :eq() - pass the value as an integer, not a string.

Also, using .getDay(), Sunday is day number 0, so you'll either need to change the order of your days, or create another work-around for this.

Upvotes: 3

mgraph
mgraph

Reputation: 15338

you should do :

$('p.day').eq(new Date().getDay()-1).addClass('today');

Upvotes: 4

gdoron
gdoron

Reputation: 150253

$('p.day').eq(new Date().getDay()).addClass('today');

Fixed DEMO

Or using the :eq psudo css selector:

$('p.day:eq(' + new Date().getDay() + ')').addClass('today');

The number should be a number and not a string.

Upvotes: 2

Related Questions