Forum Moderators: open

Message Too Old, No Replies

Finding the element that has the focus

         

NesYarug

9:23 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Hi Forum,

Is it possible to find the element that has the focus from JavaScript (preferably using standard ECMAScript) through an event handler?

What I try to do is have the user select a link (href) by means of spatial navigation and then use a key (specifically F12) to follow that link.

For example:

When the page loads I register an event handler:

<body onLoad="init();">

function init() {
document.addEventListener("keydown", handleOnKeyDown, true);
}

function handleOnKeyDown(e) {
// 123=F12
if (e.keyCode == "123") {
// Get the element that has the focus
How?
// Follow the link representing that element
How?
}
}

Any help appreciated...

Nes

NesYarug

9:30 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Found it:

function handleOnKeyDown(e) {
// 123=F12
if (e.keyCode == "123") {
// Get the element that has the focus
var element = e.target;
// Follow the link representing that element
if (element!= null) {
document.location = element;
}
}