zach
zach

Reputation: 478

Select current date on calendar with javascript and html

I created a simple calendar with html and now want the current date to highlight automatically with javascript. I know of a few ways to do this but I am looking for the most simple.

Upvotes: 0

Views: 7873

Answers (1)

Briguy37
Briguy37

Reputation: 8412

It really depends on how your calendar works. You can get the client's current date with the following JavaScript:

var currentDate = new Date();

Once you have that, you'll have to use the built in date functions to get the current date element and probably add a class to it that will style it as highlighted.

UPDATE

Assuming you have your li elements with id="dayElement_x" where x is the day number, and your class for highlighting your day is currentDay, an example JavaScript call would be:

document.getElementById('dayElement_' + (new Date()).getDate()).className += ' currentDay';

UPDATE 2

I just thought of a solution where you can do this without having a bunch of ids. Here is the JavaScript:

document.getElementById('calendarContainer').
    getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';

Upvotes: 2

Related Questions