In the standard event model tables don't receive focus. But this is a pretty close. They can at least in a relative way in IE's event model, so for IE it acts as requested. For non-IE it checks for keydown on the document and if there's a highlighted row, moves to the next. I elected to not move if none were highlighted or if no more in the given direction were available. That behavior can be changed.
Replace:
Code:
var color = "#E1E0D7"
var rows = document.getElementById("mstrTable").getElementsByTagName("tr");
var n = rows.length;
var bgcs = [];
for(var i=0; i<n; ++i) bgcs[i] = rows[i].style.backgroundColor;
function changeColor(e) {
if(!e) e = window.event;
var o = e.target? e.target: e.srcElement;
while(o.tagName && o.tagName.toLowerCase()!="tr") o = o.parentNode;
for(var i=0; i<n; ++i) {
rows[i].style.backgroundColor = bgcs[i];
if(rows[i]==o) {
rows[i].style.backgroundColor = color;
}//end of if
}//end of for
}//end of function
if(document.addEventListener) for(var i=0; i<n; ++i) rows[i].addEventListener("click", changeColor, false);
else for(var i=0; i<n; ++i) rows[i].attachEvent("onclick", changeColor);
}//end of onload
With:
Code:
var currow;
var color = "#E1E0D7"
var rows = document.getElementById("mstrTable").getElementsByTagName("tr");
var n = rows.length;
var bgcs = [];
for(var i=0; i<n; ++i) bgcs[i] = rows[i].style.backgroundColor;
function changeColor(e) {
if(!e) e = window.event;
var o = e.target? e.target: e.srcElement;
while(o.tagName && o.tagName.toLowerCase()!="tr") o = o.parentNode;
for(var i=0; i<n; ++i) {
rows[i].style.backgroundColor = bgcs[i];
if(rows[i]==o) {
currow = i;
rows[i].style.backgroundColor = color;
}//end of if
}//end of for
}//end of function
if(document.addEventListener && (window.opera || !window.attachEvent)){
for(var i=0; i<n; ++i) rows[i].addEventListener("click", changeColor, false);
document.addEventListener('keydown', function(e){
if(e.keyCode == 38){if(currow > 1)changeColor({target: rows[--currow]});}
if(e.keyCode == 40){if(currow < n - 1)changeColor({target: rows[++currow]});}
}, false);
}
else{
for(var i=0; i<n; ++i) rows[i].attachEvent("onclick", changeColor);
document.getElementById('mstrTable').attachEvent('onkeydown', function(){
if(event.keyCode == 38){if(currow > 1)changeColor({target: rows[--currow]});}
if(event.keyCode == 40){if(currow < n - 1)changeColor({target: rows[++currow]});}
});
}
}//end of onload
Bookmarks