Re: [Element Traversal LC] access to element by index

Hi, Daniel-

Daniel Glazman wrote (on 3/28/08 12:55 PM):
> 
> Ok, guys, let's see it that way : we have one given element and we
> want to find its 3rd element child. Very simple and common query,
> right ?

I'm actually not sure.  How often do authors want to get the third child 
without knowing anything more about it than that it's an element? 
Iterating through the kids (by means of ET or '.childNodes') gives you 
much more context information (what type of element it is, what it's 
bbox is, whether or not it has text/child content, etc.).  Not trying to 
be a pain, but can you identify a concrete use case?


> 2. using Selectors API :
> 
>    myFooElement.querySelector("*:nth-child(3)") does NOT work since
>    there can be another 3rd child in traversal order before the
>    3rd child of myFooElement. I have no way of querying the 3rd child
>    of myFooElement alone if myFooElement has no ID, no class, nothing
>    since the :root pseudo-class does NOT apply to myFooElement in such
>    a query but still represents the root of the document.
>    Querying  :first-child+*+*  does not work either for the same reason,
>    there can be a 3rd child deep in the subtree being before the 3rd
>    child of myFooElement in traversal order of the document...
> 
>    This is a limitation of the Selectors API I detected long ago.
>    Mentioned it a few times, too.
> 

Couldn't you do this?

var kids = myFooElement.querySelectorAll("*");
var middleChild = kids.item( 3 ); //or kids[ 3 ];

I don't want to turn this into a thread on the Selectors API, which I 
have my own issues with, but AIUI, this should work, and is pretty 
straightforward.


Regards-
-Doug Schepers
W3C Team Contact, SVG, CDF, and WebAPI

Received on Friday, 28 March 2008 17:37:30 UTC