views:

238

answers:

3

I am trying to get the values to a select list for subcategories on the basis on what is chosen in categories select list. It doesn't seem to work on IE. Can anyone suggest the problem?

In the php file I have

<body onload="setSubcategories(default_value);">

In the js file I have something like

 subcategories = new Array();
 subcategories['Lifestyle'] = ['All',
                                  'Beauty (SHC)',
                                  'Skin Care',
                                  'Hair Care',
                                  'Oral Care',
                                  'Cosmetics',
                                  'Footwear',
                                  'Jewelry',
                                  'Male Styling',
                                  'Women Hygiene',
                                  'Womens magazines',
                                  'Apparels',
                                  ' Fashion (AFA)',
                                  'Spa',
                                  'Accessories'
                                  ];
    subcategories['Automobiles'] = ['All',
                           'Automobiles (C&B)',
                           'Cars',
                           'Bikes',
                           'Car Magazine',
                           'Bikes Magazine',
                           'Accessories'
                       ];
    subcategories['FoodandBeverage'] = ['All',
                                  'Snacking',
                                  'Confectionary',
                                  'Beverages',
                                  'Generic F&B',
                                  'Restaurant Review',
                                  'Food Reviews',
                                  'Wines & Vineyards'
                              ];


function setSubcategories(default_value){
    default_value = (typeof default_value == 'undefined') ?
             'All' : default_value;
    var elem = document.getElementById('id-category');
    if(elem == null){return false;}

    var category = elem.value;
    var subelem = document.getElementById('id-subcategory');
    var html = "";
    var subcategoriesArr = subcategories[category];
    for(var i=0; i < subcategoriesArr.length; i++){
        var selected = subcategoriesArr[i] == default_value ? " selected" : "";
        html += '<option' + selected + '>' + subcategoriesArr[i] + '</option>\n';
    }
    subelem.innerHTML = html;
}
+3  A: 

Try using DOM equivalent:

var subcategoriesArr = subcategories[category];
for(var i=0; i < subcategoriesArr.length; i++){
    var option = document.createElement('option');
    option.value = option.text = subcategories[i];
    option.selected = (subcategoriesArr[i] == default_value);
    subelem.appendChild(option);
}
jerjer
Can do that but why is the above not working..I just wanted to know that.
Arkid
The above is not working because IE does not let you use innerHTML on a select.
epascarello
A: 

I've encountered this bug before. You can't set the innerHTML on a select element in Internet Explorer. You have to wrap the select element in a div (or some other element) and then generate the markup for a new select element which includes the options you want to insert.

Here's a little pseudo-code:

  1. Target the select
  2. Wrap the select in a div element
  3. Store the markup of the select for later reuse (step #5) and clear the contents of the div
  4. Generate markup for options
  5. Include that markup in the stored select markup
  6. Put the whole thing inside the div surrounding the select

Needless to say: I ended up using the pure DOM approach.. Which is a shame, since it's much slower than using innerHTML.

roosteronacid
There's no need for this. It has been possible to repopulate a select's options in all major browsers for over a decade by populating the select's `options` collection with `Option` objects.
Tim Down
@Tim Down: Try and read my answer again. I quote: _"It doesn't seem to work on IE. Can anyone suggest the problem?"._ He wants to know what the problem is. And I've given him that answer. On top of that: I am in fact _encouraging_ him to use the pure DOM approach and skip the innerHTML approach
roosteronacid
I had read and understood your answer when I commented. I have since decided my downvote was too hasty, but apparently can't remove it unless you edit your answer, so I'm sorry about that and will try and change my habits since this is the second time I've done that this week. What I objected to in your answer was the outlining of a technique we both agree is bad; I think it's better to encourage good techniques. I'm also not sure that the pure DOM approach works in all reasonable browsers but my memory of it is hazy so I could be wrong.
Tim Down
Answer edited. And I wholeheartedly agree with encouraging best practices--I do that myself (often resulting in me doing the same thing you are.. to a somewhat lesser extent).
roosteronacid
Thanks. Downvote removed, habits hopefully altered.
Tim Down
A: 

Use the time-honoured method of creating Option objects and adding them to the select's options collection:

var subcategoriesArr = subcategories[category];
var i, len, selected, optionText;

// Clear any existing options
subelem.options.length = 0;

// Create new options
for (i = 0, len = subcategoriesArr.length; i < len; i++){
  selected = (subcategoriesArr[i] === default_value);
  optionText = subcategoriesArr[i];
  subelem.options[i] = new Option(optionText, optionText, selected, selected);
}
Tim Down