Document.onkeyup ported to jQuery

I am migrating some old Javascript in jQuery:

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

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

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

however, this code does not work (the function never runs, at least in IE7 / 8). What for? How to fix?

+4
source share
1 answer

jQuery API is different:

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

jQuery.keyup is a function that takes a callback as an argument. The reason for this is to allow us to assign events with multiple keys (or something else).

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

There is also keyup () with no argument that raises the keyup event associated with the corresponding element.

+16
source

All Articles