5

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?

7 Answers 7

4

I'm currently doing this successfully by clearing the dropdown box selection so that it can be re-selected. It will allow you to trigger the function again by adding this within your function before the end:

$('select option:selected').removeAttr('selected');

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

This is possible, however it is not fully supported. Here is a sample:

html

<select id="selectId">
 <option value="1">A</option>
 <option value="2">B</option>
 <option value="3">C</option>
 <option value="4">D</option>
</select>

jquery

$("#selectId>option").click(function(){
 alert(this.value);
});

here is a mediocre approach to handle ie:

<div id="dropdownWrapper">
 <select id="selectId">
  <option value="1">A</option>
  <option value="2">B</option>
  <option value="3">C</option>
  <option value="4">D</option>
 </select>
</div>

js

var clickCount_guid324fF = 0;
$("#dropdownWrapper").click(function(){
    if(++clickCount_guid324fF % 2 == 0){
        alert($("#selectId").val());
        clickCount_guid324fF = 0;
    }
});

Comments

0

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

3 Comments

Maybe if he binds to an event of the <option> instead of the <select>
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!
Hmm, nope, binding to the option elements doesn't seem to work (in FF3.6 anyway)
0

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>

2 Comments

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…
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?
0

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.)

Comments

0

bind the change AND click event

$select.bind("change click", function (event) { // do something   });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.