Search code examples
javascriptjqueryhtmlonchange

Run change event for select even when same option is reselected


Basically, I have drop down menu that looks like this:

<select>
  <option>0</option>
  <option selected="selected">1</option>
  <option>2</option>
  <option>3</option>
</select>

I am trying to write a function that is fired even when you select the same option, i.e. even if the drop-down is opened and re-select the selected option, I want it to execute the function.


Solution

  • If you mean selecting with the mouse, you can use mouseup. However, it will fire when the select box is being opened as well, so you'll need to keep track of the amount of times it was fired (even: select is being opened, odd: select is being closed): http://jsfiddle.net/T4yUm/2/.

    $("select").mouseup(function() {
        var open = $(this).data("isopen");
    
        if(open) {
            alert(1);
        }
    
        $(this).data("isopen", !open);
    });