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

  • Home
  • SEARCH
  • 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 6108167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:16:03+00:00 2026-05-23T14:16:03+00:00

I have a list that has keyboard support for navigation. However, there is additional

  • 0

I have a list that has keyboard support for navigation. However, there is additional jiggery pokery (which isn’t inlcuded in this example) that shows and hides items in the list. After items are hidden the keyboard navigation needs to continue functioning but only for visible items.

I’ve got this working great. The problem occurs when the last visible item is the current item and then a user presses down again. The current class is removed and it tries to move on to an item that doesn’t exist. The same thing happens when navigating up.

How can I navigate visible items only and get it to stop on the first and last visible items?

jQuery(document).keydown(function(e) {

  e.preventDefault();

  if (e.keyCode == 38) { // Capture Arrow Up key 38

    var currentItem = jQuery(".options").children("li.current").removeClass("current").prevAll(':visible:first').addClass("current");

  } else if (e.keyCode == 40) { // Capture Arrow Down key 40


    var currentItem = jQuery(".options").children("li.current").removeClass("current").nextAll(':visible:first').addClass("current");
  }
});
.current {
  border: solid 1px #f60 !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul class="options">
  <li class="current"><a href="#"><span>WW Portfolio Regions</span><span class="hidden">0</span></a>
  </li>
  <li><a href="#"><span>EMEA</span><span class="hidden">1</span></a>
  </li>
  <li><a href="#"><span>Americas</span><span class="hidden">2</span></a>
  </li>
  <li><a href="#"><span>AP</span><span class="hidden">3</span></a>
  </li>
  <li><a href="#"><span>NE IOT</span><span class="hidden">4</span></a>
  </li>
  <li><a href="#"><span>SW IOT</span><span class="hidden">5</span></a>
  </li>
  <li><a href="#"><span>CCE IOT</span><span class="hidden">6</span></a>
  </li>
  <li><a href="#"><span>MEA IOT</span><span class="hidden">7</span></a>
  </li>
</ul>
  • 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-23T14:16:03+00:00Added an answer on May 23, 2026 at 2:16 pm

    If the current element is the last one, nextAll() will return an empty jQuery object.You can test for that case and only remove the class from the current element if it’s not the first (or last) one:

    jQuery(document).keydown(function(e) {
        var currentItem = jQuery(".options").children("li.current");
        if (e.keyCode == 27) {         // Capture Esc key 27
            e.preventDefault();
            closeOptions(ddOptions, thisDd);
        } else if (e.keyCode == 38) {  // Capture Arrow Up key 38
            e.preventDefault();         
            var prevItem = currentItem.prevAll(":visible:first");
            if (prevItem.length) {
                currentItem.removeClass("current");
                prevItem.addClass("current");
            }
        } else if (e.keyCode == 40) {  // Capture Arrow Down key 40
            e.preventDefault();         
            var nextItem = currentItem.nextAll(":visible:first");
            if (nextItem.length) {
                currentItem.removeClass("current");
                nextItem.addClass("current");
            }
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list that has some chapter numbers in string. When I sort
I have a drop down list that has options that need to be passed
I have a string list (TStrings) that has a couple thousand items in it.
I have a list control in Flex that has been data bound to an
I have a list of dates that a machine has worked on, but it
I have a view that has a list of jobs in it, with data
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I have a picker view that has a list of numbers to pick from.
I have an application that has a list of buttons and has customized tooltips.
I have an app that has a list of items. My customer prefers to

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.