Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect drop down openes

Is there any way (in plain JS or jQuery) to detetect exactly that moment, a drop down (select-tag) opens? To clarify more, a small example:

If you click 5 times on a select, the following happens:

drop down opens   > Event should fire
drop down closes
drop down opens   > Event should fire
drop down closes
drop down opens   > Event should fire

So far, I just can find events for the click/focus in/focus out.

like image 820
Dennis Avatar asked Dec 26 '22 19:12

Dennis


2 Answers

Look at this code:

HTML:

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js">
</script>
<select id="fire">
    <option>One</option>
    <option>Two</option>
</select>
<p></p>

JQuery:

var flag=1;
$("#fire").click(function(){
    if(flag==1){
         $("p").append("clicked   ");
        flag=0;
    } else {
         flag=1;   
    }
});
$("#fire").blur(function(){
         flag=1; 
});

jsFiddle is here

like image 105
Mohsen Safari Avatar answered Jan 12 '23 03:01

Mohsen Safari


var select = document.getElementById('mySelect');

mySelect.addEventListener('mousedown', function() {
    console.log('mousedown event fired on mySelect');
});

See this fiddle: http://jsfiddle.net/ToddT/hYT9q/

like image 26
Todd Avatar answered Jan 12 '23 05:01

Todd