Question

I have a problem with the keydown and keyup events in Opera. For some reason this is doing nothing in Opera:

// add submit event for pressing enter
$(".select2-input").each(function (counter, element) {
    $(element).keydown(function (event) {
        console.log("key down");
        if (!event) event = window.event;
        var keyCode = event.keyCode || event.which;
        if(keyCode == '13' /* enter key */ ) {
            console.log("press enter");
        }
    });
});

I've tried using keyup instead, that's also not working. In Chrome and Firefox it's working fine. As you can see the actual purpose of the code is detecting the enter key. But nevertheless there's no output coming for any key. I've seen other questions concerning only the down key in Opera, but this is apparently something else.

Thanks for your help!

Était-ce utile?

La solution

Ok I got it now.

I used Hugos answer, but changed the event to keyup and now it works perfectly fine in all of the browsers (although IE I still haven't tested).

$(document).ready(function(){
    $(document).on("keyup",".select2-input", function (event) {
        console.log("key up");
        if (!event) event = window.event;
            var keyCode = event.keyCode || event.which;
            if(keyCode == '13' /* enter key */ ) {
                console.log("press enter");
            }   
        });
});

Thanks a lot!

Autres conseils

There were bugs with this and Oper before, but should be fixed now. This may help out: http://quirksmode.org/dom/events/ Not sure what version you are using, and such.

try change your code for this:

$(document).ready(function(){
    $(document).on("keydown",".select2-input",function(event){
        console.log("key down");
        if (!event) event = window.event;
        var keyCode = event.keyCode || event.which;
        if(keyCode == '13' /* enter key */ ) {
            console.log("press enter");
        }   
    });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top