Tutorial :Javascript Event for Select element Selection


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?


Change on select boxes is unreliable anyway. Read:


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.


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');


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


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


$("#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>  


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


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


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>  


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


bind the change AND click event

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »