peter
peter

Reputation: 153

modal close button

I have a div which is in a class="modal", and I written a function in jQuery that closes this div when i press "esc" :

$(document).keypress(function (e) {
    if (e.keyCode == 27) {
        if ($('.modal:visible > .icon32').length) $('.modal:visible > .icon32')[0].click();
    }
}); 

everything works perfect in firefox, but in chrome does not, what could cause this problem?

Upvotes: 1

Views: 96

Answers (1)

DSharper
DSharper

Reputation: 3217

I have observed keypress have issues with IE as well. use keydown event instead.The keydown event happens when the key is pushed down. Immediately after that keypress event occurs. When you release key keyup event happens.

$(document).keydown(function (e) {
    if (e.keyCode == 27) {
        if ($('.modal:visible > .icon32').length) $('.modal:visible > .icon32')[0].click();
    }
});

Upvotes: 2

Related Questions