0

I tried for element .page2:

 $('.page2').on('click', 'keydown', function(e) {
   switch(e.which) {
   }  
}

But It does not work for me.

So I did error, I use now:

 $(".page2").on('keydown', function (e) {...

1 Answer 1

0

You might be trying to attach event to an element that has not been rendered yet. To avoid that, use jQuery's ready event:

$(function() {
  $('.page2').on('click, keydown', function(e) {
   switch(e.which) {
   }  
  }
});

Or, you can attach a live event instead:

$(document).on('keydown', '.page2', function(e) {
   switch(e.which) {
   }  
});

Finally, if you want to attach multiple events, you separate them by comma:

$(document).on('click, keydown', '.page2', function(e) {
   switch(e.which) {
   }  
});
2
  • It work only when I click on element div, may be to set autofocus?
    – Babaev
    Aug 2, 2015 at 19:30
  • It's hard to tell without seeing your actual HTML structure. Please consider posting it, as well as what element you want to register click events on.
    – martynasma
    Aug 2, 2015 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.