views:

183

answers:

4

Hi everyone,

I'm looking to find the Javascript Event I need to put into jQuery's .bind function in order to have the function triggered when a selection is made from a <select> element.

At the moment I'm using .bind('change',function() { ...}) but I need the event to trigger when the selected option is chosen again.

Any suggestions?

A: 

Wouldn't click work? .bind('click',function() { ...})

Dustin Laine
Maybe if he binds to an event of the `<option>` instead of the `<select>`
LeguRi
That fires when you click the dropdown too, which has frustrating results in this context. I'll try your option idea Richard, put it in a new answer!
JP
Hmm, nope, binding to the option elements doesn't seem to work (in FF3.6 anyway)
JP
+1  A: 

I once did this using mouseup, and checked where the event originated. If it was an option element, i handled the select. No listening on onchange at all:

<body>
<select id="select0">
    <option value="0">option 0</option>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

<script type="text/javascript">
$('#select0').bind('mouseup', function (e) {
    var src = e.target;
    if (src.nodeName.toLowerCase()==='option') {
        doMagicOnSelect(this);
    }
});
function doMagicOnSelect(selectElem) {
    console.log('current value:'+selectElem.value);
}
</script>
</body>
npup
Total win! Great solution, thanks!
JP
Despite its genius this solution doesn't work in Safari (at least)! I've opted for something else, but just in case anyone else wants to use it…
JP
Since the origin of the event isn't the same in (for ex. Safari), I haven't found a way to work around it and still use the style of the example above. The traditional way would be to use a "dummy" element `<option value="-1">Select something</option>` and replace the mouseup handler function with something like `function () {var value = $(this).val();if (parseInt(value)===-1) {return;} /* bailing on the dummy option */ doMagicOnSelect(this);}`. If you really need the functionality we tried to mimic, would't it make more sense to use some radiobuttons (or ordinary buttons) and act on them?
npup
A: 

Change on select boxes is unreliable anyway. Read:

http://www.quirksmode.org/dom/events/change.html#t05

I'd probably go for something click (but be suspicious, somebody (--> IE) is going to make your life difficult). Or build something yourself without select.

reto
A: 

There is no reliable event fired when a selected option is re-chosen.

Whilst on some browsers you can catch events originating from an <option> (which you could use to trap click and keyboard events if you had the patience to try to reproduce a browser's select handling), this is unstandardised and doesn't work in IE (as it uses native Windows widgets to implement select boxes, which don't give that kind of granular access).

If you need to be able to re-fire an event when the same option is chosen again, what you have doesn't really sound like a select box to me; it could perhaps be better replaced with a scripted pop-up div full of buttons. (There are plenty of scripts that will substitute a select box for a scripted div to give you greater control on browsers where JavaScript is available.)

bobince