1

I have a selectbox with multiple Options in it. I have an onchange handler on the selectbox which fires a different action depending on the selected option. This works well.

The first entry has the value 'empty' and no display text, so when the page loads, the first empty entry is selected so any other entry can be selected and used with onchange.

What i want to do is reset the selection to the empty entry everytime an action was called. I was thinking, that couldn't be so hard :-)

I tried the following:

//setting the value of the selectbox
document.getElementById('gadgetChooser').value='empty';

//setting selectIndex
document.getElementById('gadgetChooser').selectedIndex = 0;

//setting selectIndex
document.getElementById('gadgetChooser').options[0].selected = true;

None of the above mentioned ways have an effect on the actual view of the selectbox. The only thing that changes by setting the value is the value itsself that will be submitted, but the selectbox entry is not reset.

any suggestions?

1
  • Setting the selected index should work just fine: jsfiddle.net/yahavbr/JfRSQ please add more details... how you create the drop down? What is that "action" you're doing? You have any other code after this? Commented Mar 25, 2011 at 7:31

1 Answer 1

3

Is this what you mean? http://jsfiddle.net/BprVD/1/

Edit:

As requested by Chris, here's the complete solution http://jsfiddle.net/yahavbr/YjBWc/2/ c/o Shadow Wizard and myself.

Sign up to request clarification or add additional context in comments.

6 Comments

That gets close but what i mean is this: jsfiddle.net/YjBWc/1 btw: great link!
Okay, sorry, a bit confused. So you really need to use the getElementById() method?
@Chris here you go: jsfiddle.net/yahavbr/YjBWc/2 you used the getElementById in the wrong way.
@Shadow Wizard - Haha, I was going to do the same thing, but I wanted to know if that's what he was really looking for.
@Stellaire it doesn't really matter, it's two ways to achieve the same result of getting the element. What matters is how to set selected item, and your way is correct. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.