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 8414161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:05:45+00:00 2026-06-10T01:05:45+00:00

I have created a drop down that works as a calendar in javascript. I

  • 0

I have created a drop down that works as a calendar in javascript. I am trying to set up a button so that when it is clicked the id ‘date’ will display the currently selected year, month, and day. But the button does nothing. I believe I have the function set up properly, but it isn’t working and I suspect it is placed incorrectly. Can someone please help edit my code to make this work??

Feel free to play around with my jsfiddle.. http://jsfiddle.net/FQnEy/

Here is what I have tried…

<html>
<head>
<title>Life Event Picker Calendar</title>
</head>
<body>
<div id="date">Date</div>
<div id="event">Event</div>
Life Event Picker Calendar<br>
<hr align="left" width="200px"/>
--Year ------ Month ----- Day<br>
<div id="calendar-container"></div>
<br>Event: <input type="text" name="evname" /><br />
 <button onclick="getInfo()"type="button">Click Me!</button> 

<script type="text/javascript">
function getInfo() {
    document.getElementById("date").innerHTML = sel_year.value + sel_month.value + sel_day.value;
}

(function() {
    //variables and values for years
    var yr1 = 2011, yr2 = 2012, yr3 = 2013, yr4 = 2014;
    var years = [yr1, yr2, yr3, yr4];
    //array with months and associated days
    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 element 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;
    }

    //creates the values and text drop down values for years
    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 year_index = sel_year.value;
        var month_index = sel_month.value,
            df = document.createDocumentFragment();
        if ((month_index == 1) && (year_index % 4 == 0)) {
            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 + 1));
            }
        } else {
            //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 + 1));
            }
        }
        clearChildren(sel_day);
        sel_day.appendChild(df);
    }

    //this function is triggered only when you change the year
    function recalculateDays2() {
        var month_index = sel_month.value,
            df = document.createDocumentFragment();
            year_index = sel_year.value;
        //this checks to see if the month selected is Feb and the year is a leap year   
        if ((month_index == 1) && (year_index % 4 == 0)) {
            //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 + 1));
            }
        //if not a leap year and not Feb give values of normal year to all months
        } else {
            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 + 1));
            }
        }
        clearChildren(sel_day);
        sel_day.appendChild(df);
    }   
    //this creates the month values
    function generateMonths() {
        var df = document.createDocumentFragment();
        for(var i=0; i<calendar.length; i++) {
            df.appendChild(createOption(calendar[i][0], i));
        }
        //clears past months
        clearChildren(sel_month);
        //appends new months onto variable df
        sel_month.appendChild(df);
    }
    //this creates the year values
    function generateYears() {
        var df = document.createDocumentFragment();
        for(var i=0; i<years.length; i++) {
            df.appendChild(createYearOption(years[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;

    //runs the months and days functions
    generateMonths();
    generateYears();
    recalculateDays();

    //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-10T01:05:46+00:00Added an answer on June 10, 2026 at 1:05 am

    The scope of your year, month and day variables was wrong. You need to pull them out as in this jsFiddle example.

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

Sidebar

Related Questions

I have created a drop down with jQuery that can be seen here by
I have a custom styled drop-down selection box created with CSS/Javascript, which is meant
I have a form that contains 5 drop down lists which are created by
I'm trying to create a menu that works like the android notification drop-down area.
I have created a navigation menu which uses javascript. I have drop down menus
I am trying to create a drop down calendar. No jquery! Pure javascript. So
I have created a drop down menu using JQuery but when I test it
I am trying to write an MVC webpage that has two drop down lists.
I have an asp.net drop down list that has a number of items, users
I have created an HTML form with a search box and a drop down

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.