We help IT Professionals succeed at work.
Get Started

keyboard navigation

vidhubala
vidhubala asked
on
392 Views
Last Modified: 2012-05-05
i have a div container with some div cells inside.
i would like to navigate thru all internal the divs using arrow keys. the function i have works for table cells.
how do i convert to divs? pl help.
<html>
<head>
<style>	
	.high {
		background-color:green;
		color:white;
	}
</style>
<script type="text/javascript">
window.onload=function(){select(document.getElementById('a1'));};
document.onkeyup = function(e) {
	  if(!e) e = window.event;
	  switch(e.keyCode ? e.keyCode : e.charCode) {
	  case 39: //right arrow
			moveRight();
			break;
	  case 37: //left arrow
			moveLeft();
			break;
	  case 38: //up arrow
			moveUp();
			break;
	  case 40: //down arrow
			moveDown();
			break;
	  }
}
 
var prev = null;
function select(el) {
        if(prev) prev.className = "";
        el.className = "high";
        prev = el;
} 
function moveRight() {
        if(!prev) return;
        
        var next = getRightCell(prev);
        
        if(!next) {
                 return;
        }
        
        select(next);
} 
function moveLeft() {
        if(!prev) return;
        
        var next = getLeftCell(prev);
        
        if(!next) {
                return;
        }
        
        select(next);
} 
function moveUp() {
        if(!prev) return;
        
        var col = getColNum(prev);
        var nrow = getUpRow(prev);
        
        if(!nrow) {
                return;
        }
 
        var cellDiff = getCellDiff(col, nrow);
        select(nrow.cells[cellDiff]);
}
function moveDown() {
        if(!prev) return;
        
        var col = getColNum(prev);
        var nrow = getDownRow(prev);
        
        if(!nrow) {
                return;
        }
        
        var cellDiff = getCellDiff(col, nrow);
        select(nrow.cells[cellDiff]);
}
</script>
</head>
 <body>
  <div style="border:1px solid yellow;width:685px;height:160px">
		<!-- first row -->
		<div id="a1" style="border:1px solid green;height:40px;width:170px;float:left">11</div>
		<div id="a2" style="border:1px solid green;height:40px;width:340px;float:left">12</div>
		<div id="a3" style="border:1px solid green;height:40px;width:170px;float:left">13</div>
		<!-- second row -->
		<div id="b1" style="border:1px solid green;height:40px;width:150px;float:left">21</div>
		<div id="b2" style="border:1px solid green;height:40px;width:340px;float:left">22</div>
		<div id="b3" style="border:1px solid green;height:40px;width:190px;float:left">23</div>
		<!-- 3rd row -->
		<div id="c1" style="border:1px solid green;height:40px;width:200px;float:left">31</div>
		<div id="c2" style="border:1px solid green;height:40px;width:100px;float:left">32</div>
		<div id="c3" style="border:1px solid green;height:40px;width:380px;float:left">33</div>
		<!-- 4th row -->
		<div id="d1" style="border:1px solid green;height:40px;width:110px;float:left">41</div>
		<div id="d2" style="border:1px solid green;height:40px;width:90px;float:left">42</div>
		<div id="d3" style="border:1px solid green;height:40px;width:480px;float:left">43</div>
  </div>
 </body>
</html>

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE