jquery bind keyup to body in firefox
Asked Answered
C

1

11

i m binding keyup function in jquery to body which works in every browser except firefox

the code: -

 $('body').bind('keyup', function(e) {
    //alert ( e.which );
    alert('testing');

});

how do i do it for firefox. it does not responds at all

thanks

Collect answered 11/1, 2011 at 7:56 Comment(0)
H
26

bind the event to the document instead:

$(document).bind('keyup', function(e) {
    alert('testing');
});

You can make almost any node receive keyboard events. In "modern" browsers, you can setup a tabIndex. After that the event is focusable.

$(document.body).attr('tabIndex', 1).bind('keyup', function(e) {
    alert('testing');
});
Hutt answered 11/1, 2011 at 8:0 Comment(2)
Thanx it save's my time also :)Dye
Good God Andy, I can't thank you enough for this answer, I was having an awful time trying to understand why is it not working with $("body").bind(...)!Xylotomy

© 2022 - 2024 — McMap. All rights reserved.