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 7413769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:57:19+00:00 2026-05-29T06:57:19+00:00

Have following listener for keyboard ArrowDown event(it’s key code is 40 ): window.onload =

  • 0

Have following listener for keyboard ArrowDown event(it’s key code is 40):

window.onload = function() {    
var itemsContainer = document.getElementById('cities-drop');
document.addEventListener('keyup',function(event){
if (event.keyCode == 40 && itemsContainer.style.display=='block') {
event.preventDefault();
    for (var i=0;i<itemsContainer.children.length-1;i++){
        if (itemsContainer.getAttribute('class').substr('hovered')!=-1){
            itemsContainer.children[i].setAttribute('class','');
            itemsContainer.children[i].nextSibling.setAttribute('class','hovered');
                //break;
                }
            }
        }
    });

in this case hovering jumps to last element in list, after ArrowDown pressed.

In case break is uncommented,it jumps to the second element and doesn’t jumping any more.

Can’t get the principle,how to do, that listener always listens…

EDIT
live demo
perhaps,it’s a matter of closure,but i’m not certain

  • 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-29T06:57:20+00:00Added an answer on May 29, 2026 at 6:57 am

    After looking at your code and realizing what you’re trying to do, I think your error is using substr where you should be using indexOf. Here is the updated line:

    if (itemsContainer.getAttribute('class').indexOf('hovered') != -1)
    


    More detail:
    What you were actually doing was using a substr with a string value for the start index. Not sure what the result of that would be, but apparently it’s not -1, since the conditional was returning true every time, causing the next element to be hovered EVERY time, all the way down to the bottom of the list. With the break statement in there, it was executing the if-statement at the first element (causing the second element to be ‘hovered’), and then quitting.

    I would leave the break statement in there after correcting your code, so that the loop stops after it finds its match, and doesn’t loop through the rest of the items unnecessarily.


    EDIT:

    I found a couple other issues in your code as well. Here is an example that works for me in IE and FF, at least (haven’t tested in Safari, Opera or Chrome):

    <html>
    <head>
        <style type="text/css">
            .hovered
            {
                color:red;
            }
        </style>
        <script type="text/JavaScript">
            function move(event)
            {
                var itemsContainer = document.getElementById('cities-drop');
                if (event.keyCode == 40 && itemsContainer.style.display == 'block')
                {
                    if (event.preventDefault)
                        event.preventDefault();
                    if (event.cancelBubble)
                        event.cancelBubble();
                    if (event.stopImmediatePropagation)
                        event.stopImmediatePropagation();
    
                    for (var i=0; i<itemsContainer.children.length-1; i++)
                    {
                        if (itemsContainer.children[i].className.indexOf('hovered') != -1)
                        {
                            itemsContainer.children[i].className = "";
                            itemsContainer.children[i+1].className = "hovered";
                            break;
                        }
                    }
                }
            };
        </script>
    </head>
    <body onkeydown="move(event)">
        <div id="cities-drop" style="display:block;">
            <p class="hovered">Item 1</p>
            <p>Item 2</p>
            <p>Item 3</p>
            <p>Item 4</p>
            <p>Item 5</p>
            <p>Item 6</p>
            <p>Item 7</p>
        </div>
    </body>
    </html>
    

    Detail: For me, in IE9, the function was never being called. Instead, I just made it a regular function and added an onkeydown event to the body tag.

    Next, for cross-browser compatibility, you should check to make sure that event.preventDefault exists before using it. I was getting a JS error in IE.

    In your if-statement, you had itemsContainer.getAttribute('class'). Firstly, you needed to use itemsContainer.children[i]. Secondly, .getAttribute('class') didn’t work for me in IE, so I switched it to just .className.

    Lastly, itemsContainer.children[i].nextSibling didn’t work for me, but it’s simple enough to just change it to itemsContainer.children[i+1] to get the next sibling.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery event listener to get the key pressed in a
I have the following event listener in SecurityConfig security { active = true useSecurityEventListener
I have the following code in a c++ listener class (more or less), which
I have the following code to count and trigger some functions using jQuery: jQuery('#input_guideName').keyup(function(e)
I have the following code snippet to enable extension after firefox quit, observe: function
Should be straight-forward, right? We have the following listener at our disposal. chrome.tabs.onRemoved.addListener(function (tabId,
I have the following code: class Controller { public Controller(Listener audioListener, Listener videoListener) {}
I have this following javascript to activate sometime document.addEventListener('touchmove', function (e) { e.preventDefault(); },
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public

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.