I know this is old, but I thought I would put it in case someone wants alternatives. This seems ugly (at least for me), but in order to deal with how the browser handles the -1 index, this is a problem. Yes, I know this can be done better with jquery.data, but I am not familiar with this yet.
Here is the HTML code:
<select id="selected"> <option value="1">A</option> <option value="2">B</option> <option value="3">C</option> </select>
Here is the javascript code:
var currentIndex; // set up a global variable for current value $('#selected').on( { "focus": function() { // when the select is clicked on currentIndex = $('#selected').val(); // grab the current selected option and store it $('#selected').val(-1); // set the select to nothing } , "change": function() { // when the select is changed choice = $('#selected').val(); // grab what (if anything) was selected this.blur(); // take focus away from the select //alert(currentIndex); //setTimeout(function() { alert(choice); }, 0); } , "blur": function() { // when the focus is taken from the select (handles when something is changed or not) //alert(currentIndex); //alert($('#selected').val()); if ($('#selected').val() == null) { // if nothing has changed (because it is still set to the -1 value, or null) $('#selected').val(currentIndex); // set the value back to what it originally was (otherwise it will stay at what was newly selected) } else { // if anything has changed, even if it the same one as before if ($('#selected').val() == 2) { // in case you want to do something when a certain option is selected (in my case, option B, or value 2) alert('I would do something'); } } } });
source share