Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Pressing Tab to Focus on <div>

Reply
Thread Tools

Pressing Tab to Focus on <div>

 
 
mumebuhi
Guest
Posts: n/a
 
      07-21-2008
Hi,

How do you make an element (e.g. <div>) to receive focus when pressing
TAB key? I understand that some elements such as <select> and <input>
by default will receive focus when the TAB key is pressed. Is there a
way to tweak a <div> to get the same treatment?

Thank you.


Buhi
 
Reply With Quote
 
 
 
 
SAM
Guest
Posts: n/a
 
      07-21-2008
mumebuhi a écrit :
> Hi,
>
> How do you make an element (e.g. <div>) to receive focus when pressing
> TAB key? I understand that some elements such as <select> and <input>
> by default will receive focus when the TAB key is pressed.


and more useful with the attribute 'tabindex'


> Is there a way to tweak a <div> to get the same treatment?


Not in html.
perhaps could you try with an anchor in this div ?
(that doesn't work on my Fx)

What do you expect to do ?
Tab to only one specific div ?
or to jump by tabbing from div to div ?

In all cases you'll have to catch the keycode of tab,
something like:

<body onkeydown="KeyCheck(event);" onkeyup="if(cod==9)jump();">

Your div(s) must all of then have an id.

<script type="text/javascript">
var cod=0, goal, D=[];
function KeyCheck(evt) {
evt = (evt) ? evt : ((event) ? event : null);
var evver = (evt.target) ? evt.target : ((evt.srcElement)
?evt.srcElement : null );
cod = evt.keyCode;
goal = evver
}
function init() {
var d = document.getElementsByTagName('DIV');
for(var i=0, n<d.length; i<n; i++) {
d[i].idx = i;
D[i] = d[i];
}
}
window.onload = init;
function jump() {
if(goal && (!goal.tagName || goal.tagName != 'DIV')
goal = goal.parentNode;
while(goal.tagName!='DIV') goal = goal.parentNode;
var i = (goal.idx>=D.length-1)? 0 : goal.idx+1;
location = '#'+D[i].id;
}
</script>

Not tested !

--
sm
 
Reply With Quote
 
 
 
 
mumebuhi
Guest
Posts: n/a
 
      07-21-2008
Maybe a simpler question, when a <a id="a_id"> is onFocus, can we
change the focus to e.g. <div id="div_id">?

Any JavaScript object responsible to keep track what currently is on
focus?

 
Reply With Quote
 
SAM
Guest
Posts: n/a
 
      07-21-2008
mumebuhi a écrit :
> Maybe a simpler question, when a <a id="a_id"> is onFocus, can we
> change the focus to e.g. <div id="div_id">?
>
> Any JavaScript object responsible to keep track what currently is on
> focus?


the JS function focus() works only with elements of form

You can't give focus to a div, it's a nonsens.
(a div can't have an html action, so what to do with a focus ?)


but you can do :
location = "#"+'div_id'
to scroll to this div.


not tried but perhaps something like

<div id="div_id">
<a id="a_id" onfocus="var a=this.parentNode;
while(a.tagName!='DIV') a=a.parentNode; alert('div is: '+a.id);">
</div>

--
sm


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
pressing Tab jumps out of panel dwurity@gmail.com Java 1 02-11-2005 10:42 AM
Bug in Tabbrowser? (last tab always stays in tab list) Alexander Fischer Firefox 1 10-01-2004 07:40 AM
button focus and pressing enter Philip Townsend ASP .Net 2 08-25-2003 09:32 PM
button focus and pressing enter Philip Townsend ASP .Net 3 08-14-2003 07:27 PM



Advertisments