How to allow up and down arrows to navigate your form fields

October 5th, 2008 by Robert

I learned something pretty cool last week. I fielded a request from a client who wanted to be able to use the up and down arrows on his keyboard to navigate a rather large form on the site I created for him. I believed it was possible but I had never done it. So after doing some research I figured out how to do it. Below is a simplistic version (put your cursor in one of the text boxes below and push the up or down arrow keys to move between them):

1-1 1-2 1-3
2-1 2-2 2-3
3-1 3-2 3-3

The HTML for the simple table is as follows:

<table border="1">
<tr>
<td>1-1</td>
<td><input id="ItemCost11" onkeydown="return checkKey(event,3,3,'ItemCost',1,1)" maxlength="30" name="ItemCost11" size="3" /></td>
<td>1-2</td>
<td><input id="ItemCost12" onkeydown="return checkKey(event,3,3,'ItemCost',1,2)" maxlength="30" name="ItemCost12" size="3" /></td>
<td>1-3</td>
<td><input id="ItemCost13" onkeydown="return checkKey(event,3,3,'ItemCost',1,3)" maxlength="30" name="ItemCost13" size="3" /></td>
</tr>
<tr>
<td>2-1</td>
<td><input id="ItemCost21" onkeydown="return checkKey(event,3,3,'ItemCost',2,1)" maxlength="30" name="ItemCost21" size="3" /></td>
<td>2-2</td>
<td><input id="ItemCost22" onkeydown="return checkKey(event,3,3,'ItemCost',2,2)" maxlength="30" name="ItemCost22" size="3" /></td>
<td>2-3</td>
<td><input id="ItemCost23" onkeydown="return checkKey(event,3,3,'ItemCost',2,3)" maxlength="30" name="ItemCost23" size="3" /></td>
</tr>
<tr>
<td>3-1</td>
<td><input id="ItemCost31" onkeydown="return checkKey(event,3,3,'ItemCost',3,1)" maxlength="30" name="ItemCost31" size="3" /></td>
<td>3-2</td>
<td><input id="ItemCost32" onkeydown="return checkKey(event,3,3,'ItemCost',3,2)" maxlength="30" name="ItemCost32" size="3" /></td>
<td>3-3</td>
<td><input id="ItemCost33" onkeydown="return checkKey(event,3,3,'ItemCost',3,3)" maxlength="30" name="ItemCost33" size="3" /></td>
</tr>
</table>

The javascript function used is as follows:

<script type="text/javascript"><!--
function checkKey(event, numRecs, numCols, myName, myRow, myCol) {
      var key = event.keyCode;
      var bSetFocus = false;
      var myId = '';
// if the left arrow has been pressed and we're not in the first column
      if( key == 37) {
            if (myCol-1 > 0) {
                  myId =  myName + myRow + (myCol-1);
                  bSetFocus = true;
            }
      }
      // if the up arrow has been pressed and we're not in the first row
      if( key == 38) {
            if (myRow-1 > 0) {
                  myId =  myName + (myRow-1) + myCol;
                  bSetFocus = true;
            }
      }
      // if the right arrow has been pressed and we're not in the last column
      if( key == 39) {
            if (myCol+1 <= numRecs) {
                  myId =  myName + myRow + (myCol+1);
                  bSetFocus = true;
            }
      }
      // if the down arrow has been pressed and we're not in the last row
      else if( key == 40 ) {
            if (myRow+1 <= numCols) {
                  myId =  myName + (myRow+1) + myCol;
                  bSetFocus = true;
            }
      }
      if (bSetFocus) document.getElementById(myId).focus();
} // end checkKey function
// --></script>

In my code I generate the HTML using PHP so when I pass the number of records to the checkKey function (3 in this example) it is the number of rows returned from a database query, and the current row for any given row of the table is the counter in a loop I use to print the table.

By the way, if you want to allow for the left and right arrows to move left and right in a form, you can do that by expanding the checkKey function to look for key code 37 (left arrow) and key code 39 (right arrow), and you’ll need to implement a column ordering scheme (like the counter I used for the rows) so you’ll know where to take the cursor.

Four Direction Code
(submitted by Mr Arrows below, but the comment fields do not accept code so I put it in the main post. I actually had trouble putting two working code samples in the same post so I just updated the main example above to include the left and right arrows)

Related posts:

  1. The Essence of AJAX - How I learned it in an hourI was a bit resistant to learning ajax for a...

Tags:

3 Responses to “How to allow up and down arrows to navigate your form fields”

  1. mhonzh Says:

    hi…need a little help here not good in programming in php. how can i do that navigation though arrow keys in navigating table rows of record from database, meaning my onkeyup is on the tr.

    thanks…

  2. robert Says:

    Hello there,
    I just checked and it seems like having the onkeyup on the tr element is fine since it is an “intrinsic event” (as noted at http://www.w3.org/TR/REC-html40/interact/scripts.html). So in order to make it work, each tr element needs to have a unique id. In my example, the input elements have unique ids. So when you print the records from your database, initialize a counter (like $rowCount or $index or something) to equal zero, then when you print out each row increment the counter and put the counter in the id of the tr element. Or you could just use the key of the database row like this (assuming the records are in an array called $databaseRows):

    $numRecs = count($databaseRows);
    foreach ($databaseRows as $dbKey=>$dbRow) {
      echo “<tr id = ‘”.$dbKey.”‘
        onkeyup=\”return checkKey(event,’”.$numRecs.”‘,’”.$dbKey.”‘)”
        ><td>”.$dbRow[”myDataColumnLabel”].”</td></tr>”;
    }

    The code above would need to be expanded to include all your data fields, but that’s the idea. You would also need to modify the checkKey javascript function to ignore the 3rd element (the fieldname), since you will be shifting focus on the rows, not the individual table data (td) elements.

    I’m actually not sure where the cursor would go in this case though, since you are setting the focus on a table row and not an individual data element. If it doesn’t put the cursor where you want it I recommend specifying a specific td, like in my example above.
    Regards,
    Robert

  3. Mr. Arrows Says:

    This is the js code for all arrows. I needed it so I post it here.

    [code doesn’t show up well in comments so I included it in the main post -Robert]

Leave a Reply