The following code is ugly, but it does exactly what you want (I think). Basically, I intercept all onChange events and process them only if there is a corresponding onClick event, which leads to a change in value. Also note that change events are processed before click events. EDIT: just redoing this does not work in chrome, so I added browser detection code so that it only runs in firefox.
NOTE. The current code will not work if the user has placed a tab in the selection field and made their changes using the error keys, but the method below can be easily adapted to handle this case. You just need to handle key events, such as the up arrow or down arrow, or TAB or ENTER in the same way that clicks are processed below, but only if there is focus in the selection window.
NOTE 2: Playing with this more, the behavior is very strange. If you avoid the selection, the onChange event does not fire, but it is saved. If at any other time you click anywhere on the screen, the onChange event will be fired for the value that you hovered over when you escaped, although that value was actually changed as soon as you escaped. So it becomes difficult. I think you may have to handle two cases separately. One case for handling clicks and one for controlling outputs (as Patrick answered).
It gets hairy and I don't see an elegant way to code it. How about a note to the user next to the text box that says: "Your currently selected option 1 is no longer available." Then you can have a selection box with only the options available.
<select name="select" size="1" onChange="handleChange()" onClick="handleClick()" > <option>0</option> <option selected disabled>1</option> <option>2</option> <option>3</option> <option>4</option> </select> <br /> <script> var initialValue = document.getElementsByName('select')[0].selectedIndex; var potentialChange; var processClick; function handleClick() { </script> <a href="javaScript:alert(document.getElementsByName('select')[0].selectedIndex);">getSelected</a>
Jonah
source share