JQuery Keyup event not working

So, I need to use this event so that I can navigate the blog posts. I use the "J" key to go to the previous message and the "K" key to go to the next message. 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 if I press again it does nothing.

Sorry if I cannot explain this accurately enough and thanks for the help.

$(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 redirects and only those warnings, it doesn't work.

+3


source to share


2 answers


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



Thank you anyway.

+3


source


After changing the location, a new document is loaded. Document without a listener for keyup events.



+3


source







All Articles