On Jan 5, 10:17 am, Tony Andrews <tony.andrew...@googlemail.com>
> I have already posted a question about this on StackOverflow here
> (http://stackoverflow.com/questions/2006432/finding-the-first-
> focusable-item-of-any-type-within-a-div) so I won't repeat the whole
> thing.  My issue is that I want to select the first form item of any
> type (input, select etc.) within a div.  When I try $('mydiv').select
> ('input', 'select') for example, it returns all the inputs, then all
> the selects.  So the first item it returns is not necessarily the
> first item in document order: if the first item is a select and the
> scond is an input, it will return the second item first.  Any ideas?

DOM L2 HTML module defines `elements` property [1] as part of
`HTMLFormElement` interface [2], which FORM elements implement. As you
can see, `elements` returns collection of all form control elements;
that collection is of type `HTMLCollection` [3], which is a live DOM
representation of a tree. There's also an `item` method of
`HTMLCollection`, which "[...] retrieves a node specified by ordinal
index. Nodes are numbered in tree order (depth-first traversal order).

So... I think it's safe to assume that `$('myDiv').elements.item(0)`
should return a first form control element in a document order.

And of course this kind of retrieval should be an order of magnitude
faster than `selector`-based approach ;)


[1] http://web5.w3.org/TR/DOM-Level-2-HTML/html.html#ID-76728479
[2] http://web5.w3.org/TR/DOM-Level-2-HTML/html.html#ID-40002357
[3] http://web5.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75708506
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to