Search code examples
jqueryoperakeyboard-events

Opera not firing keyup and keydown events


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!


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!