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

  • Home
  • SEARCH
  • 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 8398183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:58:46+00:00 2026-06-09T20:58:46+00:00

I am trying to create a drop down calendar. No jquery! Pure javascript. So

  • 0

I am trying to create a drop down calendar. No jquery! Pure javascript. So far @rlemon has provided me great assistance and I have been able to build off code he has helped me with. I am almost finished, but am having a few small problems. So far everything works correctly, but now I am moving onto incorporating leap years which is where I am having problems. I am having trouble determining the current year that is selected. I have

sel_year.onchange = recalculateDays2(this);

which I believe is passing the value of the current year selected. Then in the function recalculateDays2 I am creating a variable

year_index = x.text;

which is supposed to be something like year_index = 2011, 2012 etc. I want the function recalculateDays2 to set feb days to 29 when the user has changed to the year 2012. Please help…

<html>
<head>
</head>
<body>
Calendar<br>
<hr align="left" width="200px"/>
--Year ------ Month ----- Day<br>
<div id="calendar-container"></div>
<script type="text/javascript">
(function() {
    var yr1 = 2011, yr2 = 2012, yr3 = 2013, yr4 = 2014;
    var years = [yr1, yr2, yr3, yr4];
    var calendar = [
        ["January", 31],["February", 28],["March", 31],["April", 30],["May", 31],["June", 30],["July", 31],["August", 31],["September", 30],["October", 31],["November", 30],["December", 31]],
        //this is the variable that accesses the content
        cont = document.getElementById('calendar-container');
    //creates the variables for the drop downs
    var sel_year = document.createElement('select'), sel_month = document.createElement('select'), sel_day = document.createElement('select');

    function createOption(txt, val) {
        //this creates the option but it seems that it is making the value -1 than what the text node is
        var option = document.createElement('option');
        option.value = val;
        option.appendChild(document.createTextNode(txt));
        return option;
    }

    function createYearOption(val) {
        var option = document.createElement('option');
        option.value = val;
        option.appendChild(document.createTextNode(val));
        return option;
    }

    //this clears any elements for days, months, years
    function clearChildren(ele) {
        while (ele.hasChildNodes()) {
            ele.removeChild(ele.lastChild);
        }
    }

    //this function is only triggered when you recalculate the months
    function recalculateDays() {
        var month_index = sel_month.value,
            df = document.createDocumentFragment();
        //l is the variable for the number of days in the month from the array above ex:28, 30, 31
        for (var i = 0, l = calendar[month_index][1]; i < l; i++) {
            //the first variable is what number will be displayed in the day drop down
            df.appendChild(createOption(i + 1, i));
        }
        clearChildren(sel_day);
        sel_day.appendChild(df);
    }

    //this function is triggered only when you change the year
    function recalculateDays2(x) {
        var month_index = sel_month.value,
            df = document.createDocumentFragment(), 
            year_index = x.text;
        //this checks to see if the month selected is Feb   
        if ((month_index == 1) && (year_index == 2012)) {
        //l is the variable for the number of days in the month from the array above ex:28, 30, 31
        for (var i = 0, l = calendar[month_index][1]; i < l + 1; i++) {
            //the first variable is what number will be displayed in the day drop down
            df.appendChild(createOption(i + 1, i));
        }
        clearChildren(sel_day);
        sel_day.appendChild(df);
    } else {}}  

    function generateMonths() {
        var df = document.createDocumentFragment();
        calendar.forEach(function(info, i) {
            df.appendChild(createOption(info[0], i));
        });
        //clears past months
        clearChildren(sel_month);
        //appends new months onto variable df
        sel_month.appendChild(df);
    }

    function generateYears() {
        var df = document.createDocumentFragment();
        years.forEach(function(i) {
            df.appendChild(createYearOption(i));
        });
        //clears past months
        clearChildren(sel_year);
        //appends new months onto variable df
        sel_year.appendChild(df);
    }

    //anytime the month selector is changed this calls the function to change the days
    sel_month.onchange = recalculateDays;
    sel_year.onchange = recalculateDays2(this);

    //i believe this automatically runs the months and days functions specifically for when first loading the page
    generateMonths();
    recalculateDays();
    generateYears();

    //this is what displays each of the individual drop downs after everything has been done to them
    cont.appendChild(sel_year);
    cont.appendChild(sel_month);
    cont.appendChild(sel_day);
}());
</script>
</body>
</html>
  • 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-06-09T20:58:48+00:00Added an answer on June 9, 2026 at 8:58 pm

    An easy way to check if a year is a leap year would be to prototype this into your code:

    Date.prototype.isLeapYear = function() {
        return (new Date(this.getFullYear(),1,29).getMonth() == 1);
    };
    

    Then you could update your array with the proper number of days in February.

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

Sidebar

Related Questions

I am trying to create a virtual javascript calendar drop down. I want the
I am trying to create a drop down list i have it working but
I am trying to create a drop down list. I have it working but
I have Xcode 4.2 and I am trying to create a drop down menu.
I am trying to create a drop down menu using jQuery and CSS. Here
Hi i am trying to create drop down menu with javascript where once i
I am trying to create a very simple javascript drop down menu. The menu
I am trying to create DependencyProperties that have a nice drop down code completion
I'm trying to create some drop down elements with CSS and jquery. Here's what
I am trying to create a drop down list using jQuery, PHP and mySQL,

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.