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

Ryan Maw rmaw at forge-industrial.com
Thu Sep 18 14:58:55 EDT 2003


Stephen Knight wrote:

> Pete Collins wrote:
>
>> Stephen Knight wrote:
>>
>>> is there a relatively painless way to get the window to change the 
>>> focus to the _next_ element without requiring knowledge of the name 
>>> of the next element?
>>>
>>> ie; as if the user had hit the tab key (the user doesn't know the 
>>> name of the next element).
>>>
>>> (this way, if I rearrange my window, I don't have to worry about 
>>> whether my code will focus in the correct order).
>>>
>>> thank you for your time.
>>
>>
>>
>>
>> Yea, this is something that keeps coming up. Take a look at this code:
>>
>>   http://www.mozdev.org/lxr/http/source/recall/package/recall.js#L55
>>
>> I need to add a general purpose routine to jslib library 
>> "commonWindow" would be a good place. A func that returns an array of 
>> all the open windows.
>
>
> If I'm reading the code correctly, it appears to be going thru the 
> browser windows and then starts looking in each window for elements 
> where id == 'content'.
>
> forgive me for being slow, but how would I find an (or the next) 
> arbitrary element in a window that was willing to accept 
> ".focus()"...by looking for a property by that name?


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!

-mawrya 

>
> _______________________________________________
> Jslib mailing list
> Jslib at mozdev.org
> http://mozdev.org/mailman/listinfo/jslib
>
>




More information about the Jslib mailing list