Double-click the event for the selected tag. problem with Firefox

I have a select element and I need to create an event when I double click . Here is the html code

<select  class='my_field' unselectable='on'  onselectstart='return false;' onmousedown='return false;'>
<option value="test">test</option>
</select> 

      

And here is the JavaScript code

 $( document ).ready(function() {


         $(".my_field").dblclick(function(e)
             {
             alert('double click');
             });

});

      

On browsers like IE or Chrome, this event works fine. But not Firefox .

I have prepared a jsfiddle . In this jsfiddle I also added some text input to show that the event works well for non-select fields, even in FF .

+3


source to share


2 answers


I'm not sure if dblclick is a standard event.

But you can easily implement it yet with code (not tested):



$(document).ready(function(){
    (function(){ // Closure. Not needed if this is the only code.
        var dblClickMs = 1000; // One second. Adjust at your needings.
        var fld = $(".my_field");
        var t0 = 0;

        fld.on("click", function(){
            var t = (new Date()).getTime();
            if (t - t0 <= dblClickMs) {
                alert('double click');
            } else {
                t0 = t;
            };
        });
    })();
});

      

+2


source


I thought jQuery was completely cross-browser, javascript seems to work and at least you don't have to simulate the actual double click event.



function doubleclick(){
    alert('double click');
}

$( document ).ready(function() {

    $(".my_field").each(function(){

        var element = this;
        if (this.addEventListener) {
            element.addEventListener('dblclick', doubleclick , false);
        } else {
            element.attachEvent('ondblclick', function() {
                return(doubleclick.call(element, window.event));   
            });
        } 
    });
});

      

0


source







All Articles