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

Hi, Henri-

Henri Sivonen wrote (on 4/1/08 10:36 AM):
> 
> c. just remove the childElementCount attribute
> 
> It seems to me that checking if an element has *any* element children is 
> going to be the most common use case for childElementCount and that can 
> be checked by checking if firstElementChild is null.
> 
> I very much like the idea having firstElementChild, lastElementChild, 
> previousElementSibling, nextElementSibling. In particular, I think using
> var e = p.firstElementChild;
> while (e != null) {
>   ...
>   e = e.nextElementChild
> }
> to iterate over child elements is a cleaner idiom than introducing an 
> index that isn't used for random access but only for forward iteration.
> 
> How often do people pick a single child by index (with a number know a 
> priori) instead of iterating over children and testing each one for an 
> interesting trait?

Again, childElementCount is not intended for index access.  It's 
intended as a preprocessing convenience for script authors.  This saves 
the costly DOM operation of having to process the loop twice in script, 
once to count the elements, and another to set values based on the total 
number of elements.

As an SVG author, this is something I've needed many times.

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

Received on Tuesday, 1 April 2008 17:13:45 UTC