0

I'm trying to hide all the buttons (or any other element I choose) by pressing the right arrow on my keyboard. Anyone know whats wrong?

$("body").keyPress(function(key){
    if (key.which == 39){
        $("button").hide();
    }
});

haha wow thanks

user3733575
  • 57
  • 1
  • 7

3 Answers3

3

its keypress(), change:

$("body").keypress(function(key){
    if (key.which == 39){
        $("button").hide();
    }
});

Better to use keydown(), as:

  1. keypress() will never be fired with Shift, Esc, and Delete &
  2. keypress() in some browser will be triggered by arrow keys but its not cross-browser
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
1

You cannot use keypress for detecting arrow keys(because it is fired only for printable characters), use keyup instead

$("body").keyup(function (key) {
    console.log(key.which)
    if (key.which == 39) {
        $("button").hide();
    }
});

Demo: Fiddle

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Try

$("body").keydown(function(e) { 

instead of KeyPress , its arrow keys .

Check jQuery keypress left/right navigation

Community
  • 1
  • 1
Rigin
  • 177
  • 2
  • 17