PDA

View Full Version : Problems making drop down box selected



jpvanzyl
10-29-2008, 01:51 PM
If Iím using javascript.
And Iím busy create a dropdown box
And I want to make a specific value the selected option. (chosen a month)
The variable "sel_value_month" contains the month that the user has chosen.
The variable "select_id_month" is the name of the dropdown's id

select_month = document.getElementById(select_id_month);

var month = new Array();
month[1] = "Jan";
month[2] = "Feb";
month[3] = "Mar";
month[4] = "Apr";
month[5] = "May";
month[6] = "Jun";
month[7] = "Jul";
month[8] = "Aug";
month[9] = "Sep";
month[10] = "Oct";
month[11] = "Nov";
month[12] = "Dec";

for(i = 1; i < 13; i++){
month_option = document.createElement('option');
month_option.setAttribute('value',i);
month_option.innerHTML = month[i];
if (sel_value_month == i) {
month_option.setAttribute( "selected", "selected");
}
select_month.appendChild(month_option);
}

Now this works perfectly in Firefox 3, and perfectly in IE7
but it does now work in IE6.
Does anybody know why?
Thanks

rdutton
10-29-2008, 02:19 PM
What I think is happening (I'm not sure without trying it myself) is that IE6 doesn't understand how to set the "selected" attribute of the <option> element before it is appended to the <select> element. I suggest setting the "selectedIndex" of the select after it has been populated, rather than creating a 'selected' attribute on the option at all.

Change this code:
--------------------------
for(i = 1; i < 13; i++){
month_option = document.createElement('option');
month_option.setAttribute('value',i);
month_option.innerHTML = month[i];
if (sel_value_month == i) {
month_option.setAttribute( \"selected\", \"selected\");
}
select_month.appendChild(month_option);
}
-----------
To this:
---------------------------------
for(i = 1; i < 13; i++){
month_option = document.createElement('option');
month_option.setAttribute('value',i);
month_option.innerHTML = month[i];
select_month.appendChild(month_option);
}
select_month.selectedIndex = sel_value_month -1;