2

I have code that creates a dropdown list but in the IE8 the dropdown has the correct number of entries but the text are blank. They do contain the correct values. I cannot see what is missing?

if(max_ch>0){
    var newDiv = $('<div>Room '+(i+1)+' <select class="adu" name="data[Rate]['+r_id+']['+ro_id+'][adults][]"></select> adults. <select class="chi" name="data[Rate]['+r_id+']['+ro_id+'][children][]"></select> children.</div>');
    newDiv.attr("id","occupants"+i).appendTo(showdiv+' .rooms_adults');

    var roomPrice = $('<input type="hidden" name="data[Rate]['+r_id+']['+ro_id+'][prices][]" value="'+room_bo+'" />');
    roomPrice.attr("id","roomprice"+i).appendTo(showdiv+' .rooms_adults');

    var num_opts = Number(max_ad)+1;
    for( ad=0; ad < num_opts; ad++){
        $(showdiv+' #occupants'+i+' select.adu').append(new Option(ad, ad));
    }
    var num_opts = Number(max_ch)+1;
    for( ch=0; ch < num_opts; ch++){
        $(showdiv+' #occupants'+i+' select.chi').append(new Option(ch, ch));                    
    }
    $(showdiv+' #occupants'+i+' select.adu').val('1');

} else {
Incognito
  • 20,537
  • 15
  • 80
  • 120
Keith Power
  • 13,891
  • 22
  • 66
  • 135
  • yes, sorry should have mentioned. I have seen information about it is something that is not supported by IE but cannot get an alternative – Keith Power Sep 02 '11 at 21:21

2 Answers2

2

You could either use

var num_opts = Number(max_ad) + 1,
    slc_adu = $(showdiv+' #occupants'+i+' select.adu');
for( ad=0; ad < num_opts; ad++){
    slc_adu.append("<option value=\"" + ad + "\">" + ad + "</option>");
}

or

var num_opts = Number(max_ad) + 1,
    slc_adu = $(showdiv+' #occupants'+i+' select.adu'),
    options = slc_adu.attr("option");
for( ad=0; ad < num_opts; ad++){
    options[options.length] = new Option(ad, ad);
}

Otherwise IE wont show the associated text values of the options.

Andreas
  • 21,535
  • 7
  • 47
  • 56
1

The Josh answer worked for me: Adding options to a <select> using jQuery?:

var o = new Option("option text", "value");
/// jquerify the DOM object 'o' so we can use the html method
$(o).html("option text");
$("#selectList").append(o);
Community
  • 1
  • 1
Alexandre L Telles
  • 3,375
  • 29
  • 26