javascript - setting a select option by another select selected option -


i trying built 3 selects when u set month select, days in day select change according month

html:

<select id="day" name="day"> </select> <select id="month" name="month" onchange="monthselected()"> </select> <select id="year" name="year"> </select> 

and on pageload:

<body onload ="adddate()"> 

the script:

ondate():

function adddate() {         addmonth();         addyear();         addday(1);     } //adddate 

the adds functions:

function addyear() {     var currentyear = new date().getfullyear();     var legalworkingage = currentyear - 16;     var select = document.getelementbyid("year");     (var = legalworkingage; >= 1900; i--) {         var option = document.createelement('option');         option.text = option.value = i;         select.add(option, 0);         }//for } //addyear function addmonth() {     var select = document.getelementbyid("month");     (var = 1; <= 12; i++) {         var option = document.createelement('option');         option.text = option.value = i;         select.add(option, 0);     } //for } //addmonth function addday(month) {     var select = document.getelementbyid("day");     (var = 0; < select.options.length; i++) {         select.options[i] = null;     }     var daynum;     switch (month) {         case 1: daynum = 31;             break;         case 2: daynum = 28;             break;         case 3: daynum = 31;             break;         case 4: daynum = 30;             break;         case 5: daynum = 31;             break;         case 6: daynum = 30;             break;         case 7: daynum = 31;             break;         case 8: daynum = 31;             break;         case 9: daynum = 30;             break;         case 10: daynum = 31;             break;         case 11: daynum = 30;             break;         case 12: daynum = 31;             break;     }//switch         (var = 1; <= daynum; i++) {         var option = document.createelement('option');         option.text = option.value = i;         select.add(option, 0);     } //for } //addday 

the monthselceted function(when change select"month"):

function monthselected() {     var month = document.getelementbyid("month");     var selectedmonth = month.options[month.selectedindex].value;     addday(selectedmonth); } 

here does: http://jsfiddle.net/pmuy3/6/ have no idea why

how fix it? tnx :d

two things wrong here

  1. before switch statement, convert month int this

    month = parseint(month);

  2. when remove options, not increase i, because when remove i-th option, next option shifts downwards , skipped, when increase i. instead, remove option 0 until array empty. can this:

    for (var = 0; select.options.length > 0;) { select.options[i] = null; }


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -