Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 466931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:31:48+00:00 2026-05-12T23:31:48+00:00

I’m creating an advent calendar and I need a piece of jQuery/Javascript that will

  • 0

I’m creating an advent calendar and I need a piece of jQuery/Javascript that will change the class on a div based on the date.

I need all previous days to have one class, the present day one class and future dates one class.

This is my source code:

<div id="container">
        <div class="box nov28">...</div>
        <div class="box dec1">...</div>
        <div class="box dec3">...</div>
        <div class="box dec5">...</div>
        <div class="box dec7">...</div>
        <div class="box dec12">...</div>
        <div class="box dec15">...</div>
        <div class="box dec17">...</div>
        <div class="box dec19">...</div>
        <div class="box dec21">...</div>
        <div class="box dec22">...</div>
        <div class="box dec23">...</div>
    </div>

It’s not a simple begin 1st December and proceed for 24 days. There are 12 days of ‘offers’ which will not follow on exactly. This can be seen by the div classes i.e. nov28 dec1 dec3

On the class days I need the class present to be added, on previous days – the class previous and on future dates – the class future.

All help appreciated.

–EDIT —

Original post amended to be more specific.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:31:48+00:00Added an answer on May 12, 2026 at 11:31 pm

    Based on the html code above

    var today = new Date();
    today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
    
    var month = "dec";
    var day = today.getDate();
    if (today.getMonth() == 10) {
        month="nov";
    }
    
    var selected = $(".container > div."+month+day);
    if (selected.size() > 0) {
        selected.prevAll("div").addClass("previous");
        selected.addClass("present");
        selected.nextAll("div").addClass("future");
    } else {
        // based on idea from Russ Cam but
        // only loop over all div's if today there is no matching div
        // as this is more costly (loop, regex, ...)
        $('.container > div').each(function() {
            var div = $(this);
            //extract month and date from class value 
            var divMonth = div.attr("class").replace(/.*(nov|dec)\d+.*/, "$1") == 'nov' ? 10 : 11;
            var divDay = div.attr("class").replace(/.*(?:nov|dec)(\d+).*/, "$1");
            var divDate = new Date(today.getFullYear(), divMonth, divDay);
            if (divDate > today)
                div.addClass('future');
            else
                if (divDate < today)
                    div.addClass('previous');
                else
                    div.addClass('present');
        });
    }
    

    Check http://jsbin.com/ewuro/edit for the demonstration code (little bit adapted to show it off). Click the output tab to try it out.

    Btw. http://jsbin.com/ewuro, although it should show the same as http://jsbin.com/ewuro/edit -> Output tab, doesn’t work. As the framework behind jsbin seems to swallow the $1 in my regex thus the three buttons for (Nov27, Dec4 and Dec8 won’t work). So just try it in the outputtab.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.