View Full Version : resetting select/option element to default option?

11-24-2012, 12:21 AM
I have a select/option element in XHTML markup.
Each option has an object method called by event handler for event 'change'
(or 'onchange').
Since the event being watched is 'change', if the user tries to run the
attactched event handler again, it will not work because there is no 'change'
event triggered.
So... how can I reset the select/option to the default value so the user has
to open the list and choose the methods to be called (so a change event is triggered)
In review of the markup pattern for select/option, one option is to be
given the attribute ... select="selected", But what would be the default
attribute/value for the parent element (select).

Thnx JK

Old Pedant
11-24-2012, 12:31 AM
???? You can't "reset" the <select> without changing what is visible to the user.

But if that is okay, then you can reset it by simply setting the selectedIndex property to zero.

This assumes, of course, that the first <option> was the one selected by default when the page first appeared.

If the page STARTED OUT with something like

<select name="frap">
<option value="731"> zamboni </option>
<option value="999"> pizza </option>
<option value="0" selected> the winner </option>
then you'd have to use document.forms[0].frap.selectedIndex=2 to reset to the initial state.

Have I utterly missed your point?

11-24-2012, 02:01 AM
Each option has an object method called by event handler for event 'change'
(or 'onchange').

That doesn't make sense - the change event applies to the entire select so you can't test for it on individual options.

If they don't change the option then there is no need to run the code attached to the change event again as nothing has changed.

If you need something to run when they move to a different field regardless of whether they changed the selected option or not then you could use the blur event.

11-26-2012, 03:58 AM
selectIndex should be the ticket.... thanx

The select/option element is to allow the user to choose a field or fields to
clear. So the change event calls the methods to clear the selected fields.
As a safe guard, I want the selected item to change back to the default,
causing the user to generate another change event to run the choice.

I could use a click event, but I think that would require a separate
button or other click-able element and another step.

If the default isn't specified, the first element is the default
Yes I want the display to revert to the default item.

<select id="(tag to use in document.getElementById())" name="(so I'll give it a name)">
<option value="none">Chose a field to clear</option>
<option value="history">clear history</option>
<option value="memory">Clear Memory</option>
<option value="both">Clear Both</option>

Old Pedant
11-26-2012, 06:20 AM

Now explain why the <select> needs an id?