1

Haven't post in quite a while.

Anyway I was searching about my subject and I got this.

$(window).keyup(function(e) {
    if (e.which === 8) {
        $('.ilol'). fadeOut();
    }
});

It's working perfectly fine. But when I change the window to a class or id it doesn't respond anymore.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Question Man
  • 85
  • 1
  • 2
  • 11
  • 3
    Have you made sure to use selectors for the jQuery? `'#id'` `'.class'` – Rimble Oct 13 '14 at 15:19
  • What element type does the ID or class belong to? Not all elements will make use of the `keyup` event. – James Donnelly Oct 13 '14 at 15:19
  • what are you trying to achive? To fadeout when hit backspace? – Manjar Oct 13 '14 at 15:24
  • I have tested a few things and noticed that it's working when the focus is in the input elements. I wonder if its possible to focus divs. And yes i want the div to fadeout when i hi backspace. – Question Man Oct 13 '14 at 17:09

3 Answers3

0

There's not a lot of detail about the type of element you are trying to attach this event handler to, but the jQuery documentation explains that form elements (e.g. input) are a safe bet because they are able to be focused across most browsers:

The keyup event is sent to an element when the user releases a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

http://api.jquery.com/keyup/

It may also be a selector issue. Make sure that your selector is working properly by pasting it in your browser's JavaScript console and see if it returns any elements.

Alex W
  • 37,233
  • 13
  • 109
  • 109
0

Make sure you are binding event in ready function.

$(document).ready(function(){
  $('.someClassName').keypress(function(e) {
      if (e.keyCode === 8) {
          $('.ilol'). fadeOut();
      }
  });
});
Mehmood
  • 933
  • 5
  • 17
0

After some testing this is what worked best for me. The 40 key in this example is the down arrow key.

$(document).keydown(function(e)
{
    if(e.keyCode == 40){
        $('.ilol').fadeOut('fast');
    }
});
Rimble
  • 873
  • 8
  • 22
  • jQuery uses `e.which` to abstract away the differences in the ways browsers detect key events. You [should use `which`](http://stackoverflow.com/questions/4471582/javascript-keycode-vs-which) instead of `keyCode`. – Alex W Oct 13 '14 at 18:23