Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Keyup event doesn't work

So, I need use this event so I can navigate trought blog posts. I use the 'J' key to go to the previous post and the 'K' key to go to the next post. My problem is that the event works on the first try but then doesn't work anymore. When i restart the browser it works if i press J or K and it redirects me to the previous/next post. But then if I press again it does nothing.

Sorry if I can't explain it exactly enought and thanks for helping.

$(document).keyup(function (event) {
   if (event.keyCode == 74) {
      var left_link = $('#nav-left a').attr('href');
      alert(left_link);
      if(typeof left_link !== 'undefined' && left_link !== false)
        window.location = left_link;
   } 
   else if (event.keyCode == 75) {
     var right_link = $('#nav-right a').attr('href');
     alert(right_link);
     if(typeof right_link !== 'undefined' && right_link !== false)
       window.location = right_link;
   }
});

Even if I don't do the redirect and only those alerts it doesn't work.

like image 949
sticksu Avatar asked Jun 20 '26 20:06

sticksu


2 Answers

After you've changed the location, a new document is loaded. A document without a listener for keyup events.

like image 56
Bergi Avatar answered Jun 22 '26 09:06

Bergi


There was a problem with the script loading, I applied a delay of 1 second and now it works just fine.

Thanks anyway.

like image 24
sticksu Avatar answered Jun 22 '26 10:06

sticksu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!