Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.onkeyup ported to jQuery

I'm porting some old Javascript to jQuery:

document.onkeyup = function (event) {
    if (!event) window.event;
    ...
}

this code works on all major browsers. My jQuery code looks like:

$(document).keyup = function (event) {
    ...
}

however this code is not working (the function is never triggered at least in IE7/8). Why? How to fix?

like image 946
dfa Avatar asked Dec 04 '22 15:12

dfa


1 Answers

The jQuery API is different:

$(document).keyup(function (event) {
    ...
});

jQuery.keyup is a function which takes as an argument the callback. The reason behind it is to let us assign multiple keyup (or whatever) events.

$(document).keyup(function (event) {
    alert('foo');
});

$(document).keyup(function (event) {
    alert('bar');
});

There's also keyup() without an argument, which will trigger the keyup event associated with the respective element.

like image 178
Ionuț G. Stan Avatar answered Dec 06 '22 06:12

Ionuț G. Stan