JavaScript Move Focus with Arrow Keys

JavaScript Move Focus with Arrow Keys
Code Snippet:Change focus on arrow keys
Author: Smith Suth
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 867
License: MIT
Edit Code online: View on CodePen
Read More

The JavaScript code snippet helps you to move focus with the keyboard arrow keys. It enables users to navigate through a table using arrow keys, specifically left, right, up, and down arrows. This code improves accessibility and ease of use for individuals who rely on keyboard navigation.

You can enhance keyboard navigation on web applications. It’s designed to be programmatically integrated into your projects that involve tabindex attribute, tables, or grids. By doing so, you can significantly improve the user experience, especially for those who rely on keyboard navigation or have mobility challenges.

How to Move Focus with Arrow Keys using JavaScript

1. First, create an HTML structure that includes your table. Ensure that each table cell (td) where you want to enable keyboard navigation has a "tabindex" attribute. Here’s an example:

<a href="#" tabindex="1">Start</a>
<p>Tab into first cell in table.</p>
<p>Then, use arrow keys to change focus on cell.</p>
<p>Once you're in a cell you can tab to focus on the links inside.</p>
<p>Then, you can tab out of the cell and resume navigating with your arrows.</p>
<table id="myTable">
  <tr id="l01">
    <td id="l01_1" tabindex="1"><a href="#">one</a>-<a href="#">x</a></td>
    <td id="l01_2" tabindex="0">two-<a href="#">x</a></td>
    <td id="l01_3" tabindex="0">three</td>
  </tr>
  <tr id="l02">
    <td id="l02_1" tabindex="0">one -<a href="#">x</a></td>
    <td id="l02_2" tabindex="0"><a href="#">two</a>-<a href="#">x</td>
    <td id="l02_3" tabindex="0"><a href="#">three</a>-<a href="#">x</td>
  </tr>
  <tr id="l03">
    <td id="l03_1" tabindex="0">one -<a href="#">x</a></td>
    <td id="l03_2" tabindex="0"><a href="#">two</a>-<a href="#">x</td>
    <td id="l03_3" tabindex="0"><a href="#">three</a>-<a href="#">x</td>
  </tr>
  <tr id="l04">
    <td id="l04_1" tabindex="0">one -<a href="#">x</a></td>
    <td id="l04_2" tabindex="0"><a href="#">two</a>-<a href="#">x</td>
    <td id="l04_3" tabindex="0"><a href="#">three</a>-<a href="#">x</td>
  </tr>
</table>

2. You can apply custom CSS to style your table and cells. (Optional)

body {
  font-family: "Arial";
}
body table td {
  padding: 10px;
  background: pink;
  width: 100px;
}

3. Copy and paste the following JavaScript code into a <script> tag within your HTML document, preferably just before the closing </body> tag. It listens for arrow key events (left, right, up, and down) within the table. When an arrow key is pressed, the code shifts the focus to the neighboring cell in the corresponding direction.

var myTable = document.getElementById('myTable');
myTable.onkeydown = function(event) {
  var numberOfCells = document.getElementById('myTable').getElementsByTagName("td").length;
  if(event.keyCode == 37) {
    console.log('left');
    document.getElementById(event.target.id).blur()
    var currentfocus = event.target.id.split('');
    currentfocus.splice(currentfocus.length-1, 1, +currentfocus[currentfocus.length - 1]-1);
    var newfocus = currentfocus.join('');
    document.getElementById(newfocus).focus()
  }
  
  ////////////
  else if (event.keyCode == 39) {
    console.log('right');
    document.getElementById(event.target.id).blur()
    var currentfocus = event.target.id.split('');
    currentfocus.splice(currentfocus.length-1, 1, +currentfocus[currentfocus.length - 1]+1);
    var newfocus = currentfocus.join('');
    document.getElementById(newfocus).focus()
  }
  ////////////
  
  else if (event.keyCode == 38) {
    console.log('up');
    document.getElementById(event.target.id).blur()
    var currentfocus = event.target.id.split('');
    currentfocus.splice(2, 1, +currentfocus[2]-1);
    var newfocus = currentfocus.join('');
    document.getElementById(newfocus).focus();
  }
  else if (event.keyCode == 40) {
    console.log('down');
    document.getElementById(event.target.id).blur()
    var currentfocus = event.target.id.split('');
    currentfocus.splice(2,1, +currentfocus[2]+1);
    var newfocus = currentfocus.join('');
    document.getElementById(newfocus).focus();
  }
};

That’s all! hopefully, you have successfully created a functionality to move focus with arrow keys. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X