views:

700

answers:

1

Yeah this works in FF and Chrome, but for some reason wont work in IE 8. I'm using a radio button to clear a section of a form.. that section is a select box, but I don't want to leave the area empty - instead I want to reset it to what it was when the page loaded. At the moment IE8 is just leaving me with an empty small select box.

Html:

<select id="city_select" disabled="true" name="location_id" onchange="show_search_button();"><option selected>Select your city</option> </select>

Javascript:

document.getElementById('city_select').innerHTML = "<option selected>Select your city</option>";

I've also tried using location_id instead of city_select in the javascript but to no avail.. innerText and innerContent dont work either.. though the inner.HTML works in IE8 for an earlier function, but that isnt trying to innerHTML into a form. Does anybody know why this works in Chrome and FF but not IE8? and is there a solution to this? Any help appreciated thanks!

+1  A: 

Try this:

document.getElementById('city_select').options.length = 0;

Then create a new option and push it onto the options array of the select. The options are a tricky bit that don't behave like other markup.

Edited to show how to create an option:

var sel = document.getElementById('city_select').options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
sel.options.push(opt);
sel.selectedIndex = 0;
Robusto
Thanks for the reply! I think I'll need to re-read it when it isnt 2.30am though, not quite sure how to do the second bit - but maybe its because I'm half asleep! Cheers anyways, hoping this works :)
Craig Whitley
Ok, I showed you one way to do it.
Robusto
Thanks for your help! unfortunately after replacing my line of code with the ones above - neither firefox, chrome or IE work now (they all just display an empty select box).. this is a right headscratcher hmmm. Thanks again though..
Craig Whitley
It'd be still interesting to know how to do this - but instead of resetting the select box, I've just hidden the select boxes altogether (both from default and when resetting) which works nicely :) Cheers for your time though :)
Craig Whitley
Instead of sel.options.push(opt), try sel.options[0] = opt.
Robusto