Results 1 to 2 of 2

Thread: Problems making drop down box selected

  1. #1
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Problems making drop down box selected

    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

  2. #2
    Join Date
    Aug 2007
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •