best practice for navigating through a table with arrow keys?

P

PJ6

When I hit an arrow key, I want to be able to change the focus from cell to
cell just like in Excel. I see two ways of doing this. First, emitting in
script a function call in each cell on key down that specifically lists the
ID's of the adjacent controls. The second is to somehow put in some
navigation information into the ID's themselves and then loop through them
on key press to try to calculate which cell to go to next. Which is the
better technique? Are there others that may be better?

TIA,
Paul
 
M

McKirahan

PJ6 said:
When I hit an arrow key, I want to be able to change the focus from cell to
cell just like in Excel. I see two ways of doing this. First, emitting in
script a function call in each cell on key down that specifically lists the
ID's of the adjacent controls. The second is to somehow put in some
navigation information into the ID's themselves and then loop through them
on key press to try to calculate which cell to go to next. Which is the
better technique? Are there others that may be better?

TIA,
Paul


Will this help? Watch for word-wrap. Test "as-is".

<html>
<head>
<title>arrows.htm</title>
<script type="text/javascript">
var b4 = "";
var col = 1;
var row = 1;
document.onkeyup = function() {
if (window.event) {
var key = window.event.keyCode;
window.status = key;
if (key == 37) { // left arrow
if (col > 1) col--;
} else if (key == 38) { // up arrow
if (row > 1) row--;
} else if (key == 39) { // right arrow
if (col < 5) col++;
} else if (key == 40) { // down arrow
if (row < 5) row++;
}
bg();
}
}
function bg() {
var rc = "r" + row + "c" + col;
if (b4 == "") b4 = rc;
document.getElementById(b4).style.backgroundColor = "white";
document.getElementById(rc).style.backgroundColor = "yellow";
b4 = rc;
}
</script>
</head>
<body onload="bg()">
<table border="1" width="500">
<tr height="75">
<th bgcolor="#FFFFFF" width="75"><sup>Use<br>Arrow<br>Keys</sup></th>
<th bgcolor="#EEEEEE" width="75"><sup>Col 1</sup></th>
<th bgcolor="#EEEEEE" width="75"><sup>Col 2</sup></th>
<th bgcolor="#EEEEEE" width="75"><sup>Col 3</sup></th>
<th bgcolor="#EEEEEE" width="75"><sup>Col 4</sup></th>
<th bgcolor="#EEEEEE" width="75"><sup>Col 5</sup></th>
</tr>
<tr height="75">
<th bgcolor="#EEEEEE"><sup>Row 1</sup></th>
<th id="r1c1">&nbsp;</th>
<th id="r1c2">&nbsp;</th>
<th id="r1c3">&nbsp;</th>
<th id="r1c4">&nbsp;</th>
<th id="r1c5">&nbsp;</th>
</tr>
<tr height="75">
<th bgcolor="#EEEEEE"><sup>Row 2</sup></th>
<th id="r2c1">&nbsp;</th>
<th id="r2c2">&nbsp;</th>
<th id="r2c3">&nbsp;</th>
<th id="r2c4">&nbsp;</th>
<th id="r2c5">&nbsp;</th>
</tr>
<tr height="75">
<th bgcolor="#EEEEEE"><sup>Row 3</sup></th>
<th id="r3c1">&nbsp;</th>
<th id="r3c2">&nbsp;</th>
<th id="r3c3">&nbsp;</th>
<th id="r3c4">&nbsp;</th>
<th id="r3c5">&nbsp;</th>
</tr>
<tr height="75">
<th bgcolor="#EEEEEE"><sup>Row 4</sup></th>
<th id="r4c1">&nbsp;</th>
<th id="r4c2">&nbsp;</th>
<th id="r4c3">&nbsp;</th>
<th id="r4c4">&nbsp;</th>
<th id="r4c5">&nbsp;</th>
</tr>
<tr height="75">
<th bgcolor="#EEEEEE"><sup>Row 5</sup></th>
<th id="r5c1">&nbsp;</th>
<th id="r5c2">&nbsp;</th>
<th id="r5c3">&nbsp;</th>
<th id="r5c4">&nbsp;</th>
<th id="r5c5">&nbsp;</th>
</tr>
</table>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top