Jump to content

Dynamic Data


ChompGator

Recommended Posts

Would anyone know a script for having three drop down boxes

One for: Year

One for: Month

One for: Day

 

And the data populates based on the month selected.

Ie: If I select 2008 > Then February > It pre-populates 29 days because this year February only had 29 days.

 

If I selected 2008 > Then March > Then 31 days are pre-populated into the drop down because March had 31 days.

 

Anyone have a simple script to do that? :(

 

 

Link to comment
Share on other sites

no one is gonna write it for you but we can give insight on how to do it

 

you can make use of the javascript built in date functions.

Then based on what was clicked, use the functions to find out the year, and using math and the fact that each month as the same number of days every 4 years, you can calculate what this year will have.

 

have fun making it :)

Link to comment
Share on other sites

...and using math and the fact that each month as the same number of days every 4 years, you can calculate what this year will have.

 

Ah, but leap year doesn't occur every four years. It occurs every four years EXCEPT on any year that is divisible by 100 (unless it is also divisible by 400).

 

2000 = Leap year

2100 = Not a leap year

2200 = Not a leap year

2300 = Not a leap year

2400 = Leap year

 

Anyway, here is some code for ya

<html>
<head>

<script type="text/javascript">

  var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  function changeDays() {

    var year  = parseInt(document.getElementById('year').value);
    var month = parseInt(document.getElementById('month').value);
    var day   = document.getElementById('day');

    //Determine number of days in the month
    if (month==2 && (year%4==0 && (year%100!=0 || year%400==0))) {
      //Feb in a leap year
      var daysInMonth = 29;
    } else {
      var daysInMonth = monthDays[month-1];
    }

    //Add days (if needed)
    while (daysInMonth>day.length) {
      day.options[day.length] = new Option((day.length+1), (day.length+1));
    }

    //remove days (if needed)
    day.length = daysInMonth;

    return;
}

</script>

</head>

<body onload="changeDays();">

Month:
<select id="month" name="month" onchange="changeDays()">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
</select>
Day:
<select id="day" name="day"></select>

Year:
<select id="year" name="year" onchange="changeDays()">
    <option value="2000">2000</option>
    <option value="2001">2001</option>
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2004</option>
    <option value="2005">2005</option>
    <option value="2006">2006</option>
    <option value="2007">2007</option>
    <option value="2008">2008</option>
    <option value="2009">2009</option>
    <option value="2010">2010</option>
</select><br>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.