Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Is there a way to focus element from querySelectorAll()?

I want to focus my dropdown elements from keyboard (up and down keys).

My code:

var matches = document.querySelectorAll("div.dropdown-content > a");
var i = 0;
var focused = matches[i];

document.onkeydown = function(e) {
    switch (e.keyCode) {
        case 37:
            i++;
            focused.focus();
            break;
        case 38:
            i--;
            focused.focus();
            break;
    }
};

But it's actually not working. Console does not report any errors.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Arrow key up is 38, down is 40, also check i range between 0, nodes.length

const matches = document.querySelectorAll('div.dropdown-content > a');
let i = 0;
let focused;
if (matches.length > 0) {
matches[i].focus();
focused = matches[i];
document.onkeydown = function(e) {
    e.preventDefault();
    switch (e.keyCode) {
        case 40:
            if (i < matches.length - 1) {
                i++;
            }
            matches[i].focus();
            focused = matches[i];
            break;
        case 38:
            if (i > 0 ) {
                i--;
            }
            matches[i].focus();
            focused = matches[i];
            break;
    }
};
}
a { display: block; }
<div class="dropdown-content">
 <a href="#">Link</a>
 <a href="#">Link</a>
 <a href="#">Link</a>
 <a href="#">Link</a>
 <a href="#">Link</a>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You keep focusing the same element, focused is not updated when i changes.

Also, the keycode for down arrow is 40, not 37.

Finally, you should check that i is always between 0 and matches.length - 1.

var matches = document.querySelectorAll("div.dropdown-content > a");
var i = 0;

document.onkeydown = function(e) {
    switch (e.keyCode) {
        case 40:
            if(i < matches.length - 1) i++;
            matches[i].focus();
            break;
        case 38:
            if(i > 0) i--;
            matches[i].focus();
            break;
    }
};

You can also focus the first item when you reached the last one and vice versa :

document.onkeydown = function(e) {
    switch (e.keyCode) {
        case 40:
            i = (i + 1) % matches.length;
            matches[i].focus();
            break;
        case 38:
            i = Math.abs(i - 1 + matches.length) % matches.length;
            matches[i].focus();
            break;
    }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!