[Jslib] forcing focus to next element...whatever it is.

Stephen Knight stknight at ultralifebatteries.com
Thu Sep 18 18:06:03 EDT 2003


Ryan Maw wrote:

[ snip ]

> 
> 
> Yes, this type of focusing is possible...
> You just want to walk the DOM tree or your document or window to the 
> next appropriate element and focus on it.  Some elements might not be 
> appropriate to focus on so you need to make a list, for example ()  If  
> you had:
> 
> <html>
>   <head>
>    <script>
>       var currentFocusElement = null;
>       function onTabPress(){
>         if(currentFocusElement.hasChildren()){
>            ...some code here to cycle through the children nodes until 
> it finds a child node of type=1  (an element node)...  (you could also 
> put in a filter to select only element nodes with certain names, ie 
> <input>, <select>, etc...)
>            ... once you have found one focus on it!
>            return true;
>         }
>         ...IF THE ABOVE DOESN'T FIND ANY CHILD ELEMENTS TO FOCUS, THEN 
> WE CYCLE THROUGH SIBLINGS BELOW USING .nextSibling
>         ... read about DOM API properties methods like: .lastChild, 
> .nextSibling, .hasChildren in the Mozilla or W3C documentation.
>                ...       }
>    </script>
>   </head>
>  <body onfocus="var currentFocusElement=event.target">  //This part 
> grabs the element reference when that element is focused on.  (Because 
> the focus event bubbles up the body element you can capture the event 
> there and assign the element that caused the event to a variable.  You 
> then use this variable as a reference to the starting element when 
> someone hits the tab key.)
>  </body>
> </html>
> 
> Basically: do some home work on walking a DOM (document object model) 
> using javascript.  Any XML tutorial should teach you about DOMs and 
> Nodes and the different types of nodes --  this is where you should start.
> 
> Hope thats not too confusing!

nope.  I figure between parentNode and nextSibling, walking isn't too bad.

Since the window already does this, I was just hoping for a "ready-to-use" routine already 
available.  (insert buzzwords about "reusability", etc. here).

thanks.



More information about the Jslib mailing list