Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 573761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:45:30+00:00 2026-05-13T13:45:30+00:00

I have successfully implemented the scrollTo jQuery plugin which scrolls to the next div

  • 0

I have successfully implemented the scrollTo jQuery plugin which scrolls to the next div with the class “new” when a link is clicked. However, I would also like to be able to use the arrow keys to scroll up and down to the next/previous divs of the same class.

I have looked all over the internet but have been unable to find out how to do this. I am very new to JS so very simple instructions would be appreciated!

Here is the relevant code:

<script type="text/javascript">
jQuery(function($){

 $('<div id="next_arrow"></div>') 
  .prependTo("body") //append the Next arrow div to the bottom of the document
  .click(function(){ 
   scrollTop = $(window).scrollTop();
   $('.new').each(function(i, h2){ // loop through article headings
    h2top = $(h2).offset().top; // get article heading top
    if (scrollTop < h2top) { // compare if document is below heading
     $.scrollTo(h2, 800); // scroll to in .8 of a second
     return false; // exit function
    }
   });
  });

});
</script>

What do I need to add to this to make the arrow keys work?

Thanks,
Ted

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:45:31+00:00Added an answer on May 13, 2026 at 1:45 pm

    You can use the keydown event listener to listen for keypresses. You can use this on &lt;input&gt; fields and the like. Because keydown events bubble up the DOM, you can use it on the document object to catch any keypress on the page:

    $(function () {
      $(document).keydown(function (evt) {
        alert("Key pressed: " + evt.keyCode);
      });
    });
    

    Each keypress has a code. If you use the code above in your web page, you’ll see that the key code for the down arrow is 40. You can solo this out using an if or switch statement in the handler:

    jQuery(function () {
    
      $(document).keydown(function (evt) {
        if (evt.keyCode == 40) { // down arrow
          alert("You pressed down.");
        }
      });
    
    });
    

    Now you need to bind in the code that actually jumps to the next heading. I recommend abstracting the code out into a function so you can use it for both keypresses and clicks. Here is the function, together with a variant of your original code that uses it:

    // Here is the function:
    
    function scrollToNew () {
      scrollTop = $(window).scrollTop();
      $('.new').each(function(i, h2){ // loop through article headings
        h2top = $(h2).offset().top; // get article heading top
        if (scrollTop < h2top) { // compare if document is below heading
          $.scrollTo(h2, 800); // scroll to in .8 of a second
          return false; // exit function
        }
      });
    }
    
    // Here is your original code, modified to use the function:
    
    jQuery(function () {
    
      $("#next").click(scrollToNew);
    
    });
    

    Finally, you can add in the keypress code and call the function from there:

    function scrollToNew () {
      scrollTop = $(window).scrollTop();
      $('.new').each(function(i, h2){ // loop through article headings
        h2top = $(h2).offset().top; // get article heading top
        if (scrollTop < h2top) { // compare if document is below heading
          $.scrollTo(h2, 800); // scroll to in .8 of a second
          return false; // exit function
        }
      });
    }
    
    jQuery(function () {
    
      $("#next").click(scrollToNew);
    
      $(document).keydown(function (evt) {
        if (evt.keyCode == 40) { // down arrow
          evt.preventDefault(); // prevents the usual scrolling behaviour
          scrollToNew(); // scroll to the next new heading instead
        }
      });
    
    });
    

    Update: To scroll upwards, do two things. Change the keydown handler to:

      $(document).keydown(function (evt) {
        if (evt.keyCode == 40) { // down arrow
          evt.preventDefault(); // prevents the usual scrolling behaviour
          scrollToNew(); // scroll to the next new heading instead
        } else if (evt.keyCode == 38) { // up arrow
          evt.preventDefault();
          scrollToLast();
        }
      }
    

    and write a scrollToLast() function based off of scrollToNew() that finds the last new heading that isn’t on the page:

    function scrollToLast () {
      scrollTop = $(window).scrollTop();
    
      var scrollToThis = null;
    
      // Find the last element with class 'new' that isn't on-screen:
      $('.new').each(function(i, h2) {
        h2top = $(h2).offset().top;
        if (scrollTop > h2top) {
          // This one's not on-screen - make a note and keep going:
          scrollToThis = h2;
        } else {
          // This one's on-screen - the last one is the one we want:
          return false;
        }
      });
    
      // If we found an element in the loop above, scroll to it:
      if(scrollToThis != null) {
        $.scrollTo(scrollToThis, 800);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 363k
  • Answers 363k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Answer based on first revision. Typically applications would give this… May 14, 2026 at 3:22 pm
  • Editorial Team
    Editorial Team added an answer You could try with 2 rewrite rules RewriteEngine On RewriteCond… May 14, 2026 at 3:22 pm
  • Editorial Team
    Editorial Team added an answer This can be a workaround: Create a named resource: map.toCSV… May 14, 2026 at 3:22 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.