W3C home > Mailing lists > Public > whatwg@whatwg.org > February 2006

[whatwg] getElementsByClassName()

From: ROBO Design <robodesign@gmail.com>
Date: Fri, 03 Feb 2006 15:33:55 +0200
Message-ID: <op.s4efytzkmapogm@localhost.localdomain>
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
Received on Friday, 3 February 2006 05:33:55 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:26 UTC