views:

7020

answers:

5

I have an input form that lets me select from multiple options, and do something when the user changes the selection. Eg,

<select onChange="javascript:doSomething();">
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>

Now, doSomething() only gets triggered when the selection changes.

I want to trigger doSomething() when the user selects any option, possibly the same one again.

I have tried using an "onClick" handler, but that gets triggered before the user starts the selection process.

So, is there a way to trigger a function on every select by the user?

Update:

The answer suggested by Darryl seemed to work, but it doesn't work consistently. Sometimes the event gets triggered as soon as user clicks the drop-down menu, even before the user has finished the selection process!

+1  A: 

Just an idea, but is it possible to put an onclick on each of the <option> elements:

<select>
  <option onclick="doSomething(this);">A</option>
  <option onclick="doSomething(this);">B</option>
  <option onclick="doSomething(this);">C</option>
</select>


Another option could be to use onblur on the select. This will fire anytime the user clicks away from the select. At this point you could determine what option was selected. To have this even trigger at the correct time, the onclick of the option's could blur the field (make something else active or just .blur() in jQuery).

Darryl Hein
darn! "why didn't I think of that" moment. Thanks!
ePharaoh
@Darryl This didn't actually work as expected. Sometimes the event gets triggered even before user completes the selection. See the update that I added to the question.
ePharaoh
Putting event handlers on the options will fail in all versions of IE! http://webbugtrack.blogspot.com/2007/11/bug-280-lack-of-events-for-options.html
scunliffe
A: 

You can use onChange: http://www.w3schools.com/TAGS/tag_Select.asp

Copied from http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.html onChange designates a JavaScript to run when the user chooses one of the options. This means that an action is initiated immediately when the user chooses an item, not when a "submit" button is pressed.

vmarquez
As mentioned in the question, this does not work as it is only when the value is changed, not when any event is selected including the currently selected one.
Darryl Hein
Glad I'm not the only one who misread the question.
cletus
A: 

Actually, the onclick events will NOT fire when the user uses the keyboard to change the selection in the select control. You might have to use a combination of onChange and onClick to get the behavior you're looking for.

Brandon Montgomery
Yes, I would have to.. but the problem I am facing is a separate one orthogonal to the concern you raise. The onclick event on <option> is getting fired even before the user completes his selection, which seems to make it impossible to use.
ePharaoh
A: 

Kindly note that Event Handlers are not supported for the OPTION tag on IE, with a quick thinking..I came up with this solution, try it and give me your feedback:

<script>
var flag = true;
function resetIndex(selObj) {
    if(flag) selObj.selectedIndex = -1;
    flag = true;
}
function doSomething(selObj) {
    alert(selObj.value)
    flag = false;
}
</script>
<select onchange="doSomething(this)" onclick="resetIndex(this)">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>

What I'm doing here actually is resetting the select index so that the onchange event will be triggered always, true that you we lose the selected item when you click and it maybe annoying if your list is long, but it may help you in someway..

+1  A: 

If you really need this to work like this, I would do this (to ensure it works by keyboard and mouse)

  • Add an onfocus event handler to the select to set the "current" value
  • Add an onclick event handler to the select to handle mouse changes
  • Add an onkeypress event handler to the select to handle keyboard changes

Unfortunately the onclick will run multiple times (e.g. on onpening the select... and on selection/close) and the onkeypress may fire when nothing changes...

<script>
  function setInitial(obj){
    obj._initValue = obj.value;
  }
  function doSomething(obj){
    //if you want to verify a change took place...
    if(obj._initValue == obj.value){
      //do nothing, no actual change occurred...
      //or in your case if you want to make a minor update
      doMinorUpdate();
    } else {
      //change happened
      getNewData(obj.value);
    }
  }
</script>


<select onfocus="setInitial(this);" onclick="doSomething();" onkeypress="doSomething();">
  ...
</select>
scunliffe