[whatwg] getElementsByClassName()

ROBO Design robodesign at gmail.com
Fri Feb 3 05:33:55 PST 2006


On Fri, 03 Feb 2006 14:34:14 +0200, Gervase Markham <gerv at mozilla.org>  
wrote:

> Shadow2531 wrote:
<...>
>
> This seems like a sensible change. Call it getElementsByClassNames()
> would make it obvious that if you supply multiple class names, you get
> only elements with all those names. And it would be a reasonably obvious
> reduction that if you just supply a single name, you would get all
> elements which had that one class name.
>
> So we've ended up with:
>
> elem.getElementsByClassNames("foo");
> and
>
> elem.getElementsByClassNames(["foo", "bar"]);
> or
> elem.getElementsByClassNames("foo", "bar");
> or both.
>
> Are there similar functions in the DOM at the moment which can take
> multiple arguments? Do you pass an array or multiple individual
> arguments, or can you do both?
>
> Gerv

I believe there's some disagreement on what is this function supposed to  
do.

Example:

<p class="foo bar sample">paragraph 1
<p class="foo sample">paragraph 2
<p class="bar sample">paragraph 3
<p class="sample">paragraph 4

1. Should it return *all* elements which have *all* the class names wanted?

getElementsByClassNames(["foo", "sample"])
returning: p1, p2

2. Should it return *all* elements which have *only* the class names  
wanted?

getElementsByClassNames(["sample", "foo"])
returning: p2

3. Should it return *all* elements which have *any* of the class names  
wanted?

getElementsByClassNames(["sample", "foo"])
returning: p1, p2, p3, p4

4. Should the order matter?

It is clear that getElementsByClassNames("foo bar") can't be an option,  
because the reasons have already been over-stated.

Now... what's left to discuss: what is the function supposed to do? And  
... to pick one of the following two methods of giving the list of class  
names:

getElementsByClassNames("foo", "bar")
or...
getElementsByClassNames(["foo", "bar"])

As I previously stated, I preffer the latter method because we must be  
able to provide an arbitrary number of class names without using the  
(AFAIK deprecated) eval().

I also believe this function will always be confusing, no matter what. To  
drop all confusion just rename it to getElementsByCSSselector() and you  
get the exact idea to as what you are supposed to provide as an argument  
(if you know CSS). Yet, this is something Ian Hickson said is beyond the  
purpose of WHATWG.


-- 
http://www.robodesign.ro
ROBO Design - We bring you the future



More information about the whatwg mailing list