View Full Version : reset select back to first option

02-13-2009, 02:56 AM
Ok I have 4 select fields where they use ajax to pull in the options for the next select. If I change the first select after I already picked option for all four I want to reset the 3 under it to the first one back to the first option for each select.

so on each select I have onchange I call a my function


and my function looks like this.

function setDefault(div) {
var priceID = document.getElementsByName('price');
var makeID = document.getElementsByName('make');
var modelID = document.getElementsByName('model');

if(div = 'type') {
priceID.selectedIndex = 0;
makeID.selectedIndex = 0;
modelID.selectedIndex = 0;

/*priceID.selected = priceID.item(1);
makeID.selected = makeID.item(1);
modelID.selected = modelID.item(1);*/
if(div = 'price') {
makeID.selected = makeID.item(1);
modelID.selected = modelID.item(1);
if(div = 'make') {
modelID.selected = modelID.item(1);

I don't know very much about javascript so I don't even know if I'm going about this the correct way. I hope I explained what i'm trying to do well enough. Any help is really appreciated. thanks

02-16-2009, 08:06 AM
just repopulate all three through ajax, or have an if statement in javascript that selects all 3 pulldowns to the first option.

you may just be looking for - document.yourformname.yourselectmenu.selectedIndex = "0";

I'd have written "document.getElementById", but it's not DOM proper, and I always get caught for my none-dommyness.
I feel "selectedIndex" was the missing piece to your puzzle.

check this out for more info - http://www.mredkj.com/tutorials/tutorial002.html

02-16-2009, 08:16 AM
thanks for the info. I ended up going with a different set up any way but thank you for the info I'm definitely going to look into the tutorial you should me for future reference. thanks again.