0
votes

I have a basic form with 3 drop-down boxes

<form>
<select name="year" id="y">
<option value="2014">2014</option>
...
<option value="1949">1949</option>
</select>
<select name="month" id="m">
<option value="01">JAN</option>
...
<option value="12">DIC</option>
</select>
<select name="day" id="d">
<option value="1">1</option>
...
<option value="31">31</option>
</select>
<input type="text" name="age">
</form>

So the idea is: Get the months so that the select options for days can be populated, 1,2,3,4,5... and so on... then once I have the Year, Month and the day, I need to make a calculation so that I can get the Age, the value/result from the calculation will go to the input box for age... Now, for the years has to be no less than 1 year and no more than 69 or 70 years old, which I have done that already...:

function getYears()
    {
        d = new Date();
        curr_year = d.getFullYear();

        for(i = 0; i < 70; i++)
        {
            document.getElementById('y').options[i] = new Option(curr_year-i,curr_year-i);
        }
    }

With that code I'm able to populate the options for the year... and with the next code I'm able to get the number of days as per month selected, but is not working...

It's not working because if I select January it displays 31 days in the days <select>, but if I then change for another month it will keep showing the number of months from the first selection...

function obtenDias(v) {
    var select = document.getElementById("d");
    switch(v)
    {
        case "01":  ed = 31;break;
        case "02":  ed = 29;break;
        case "03":  ed = 31;break;
        case "04":  ed = 31;break;
        case "05":  ed = 31;break;
        case "06":  ed = 31;break;
        case "07":  ed = 31;break;
        case "08":  ed = 31;break;
        case "09":  ed = 31;break;
        case "10":  ed = 31;break;
        case "11":  ed = 31;break;
        case "12":  ed = 31;break;
    }
    for(i=1;i<=ed;i++)
    {
        select.options[i] = new Option(i, i, true, false);
    }
}

As for the calculation to get the age base on the selections... well, I have no clue... I can get the current date minus selected year... something like that.. it doesn't need to be 100% exact...

1
Its will return no of days in particular month and year ******* function daysInMonth(month,year) { var dd = new Date(year, month, 0); return dd.getDate();}****Syed Mohamed

1 Answers

1
votes

That's because you are not clearing the old options.

So, for Jan selection, you created 31 option items. But for Feb selection, you are updating 29 options still keeping the 30th and 31st option item. So, remove the extra options or simply remove all options before repopulating.

Try this before re-populating your options list for days:

while(select.options.length > 0){
    selection.options.remove(0);
}