Dan LaManna
Dan LaManna

Reputation: 3501

Changing a select's values based on the value of another select

I have three selects, month, day, and year. For this, year is irrelevant. I wrote a script so that on change of the month, it changes the values of the days select so that if they chose 2 as a month, it would change it to only 28 days for february.

Upvotes: 0

Views: 392

Answers (1)

RSK
RSK

Reputation: 17516

please clear all the options before creating a new options.

<script type="text/javascript">
    function pickdata(month){
        day = document.getElementById('day');
        len = day.options.length;
        for(i=len-1;i>=0;i--)
        {
        day.remove(i);
        }
        switch(month){
            case "2":
                max = 30;
            break;
            case "4":
            case "6":
            case "9":
            case "11":
                max = 31
            break;
            case "1":
            case "3":
            case "5":
            case "7":
            case "8":
            case "10":
            case "12":
               max=32;
            break;
        }
         for(i=1;i< max ; i++){
             var elOptNew = document.createElement('option');
            elOptNew.text = i;
            elOptNew.value = i;
            day.appendChild(elOptNew);
        }
    }
</script>

<select name="month" id="month" onchange="pickdata(this.value)">

Upvotes: 1

Related Questions