[whatwg] getElementsByClassName()

Lachlan Hunt lachlan.hunt at lachy.id.au
Mon Sep 5 01:43:10 PDT 2005

Aankhen wrote:
> On 9/5/05, Lachlan Hunt <lachlan.hunt at lachy.id.au> wrote:
>>1. Equivalent to ("foo", "bar") (or [class~=foo][class~=bar], or
>>    .foo.bar in CSS)
>>2. The way it currently works. ie. matches "foo bar", not "bar foo"
>>3. Error, return nothing.
> I suggest #2, which implies consistently treating the first argument
> passed to the function as a single class name to match (this means
> "foo bar" would always return no elements,

No, as already demonstrated, #2 does return matches in some cases. 
However, after some more testing, the results are inconsistent among 
browsers, which makes that alternative inappropriate.  For example, 
compare the results of these three tests in different browsers:  (I 
tested Firefox, Opera and IE on Windows).


007: Firefox: E and F, Opera and IE: E
011: All: (none)
012: Firefox: (none), Opera and IE: F

The only difference between the tests is the white space in the 
parameter.  The first uses a single space, the second uses multiple 
spaces and the third uses a space followed by a tab.

> Special-casing "foo bar" and other values seems to be adding
> complexity without much return.

It's not about special casing, it's about defining error recovery 
consistently between implementations.  As it's currently defined, ("foo 
bar" is, I believe, erroneous since each parameter represents a single 
class name.  However, the results from different implementations should 
be identical, which is why the behaviour needs to be defined and why #2 
is not appropriate.

That leaves the choice between #1 and #3 (or possibly #4 if someone 
thinks of another).  I have no strong opinion either way, I just need it 
defined so I can implement it.

> If multiple class names really need to be handled, my suggestion would
> be to take a single array as a parameter, e.g.
> `getElementsByClassName(["foo"])` and `getElementsByClassName(["foo",
> "bar"])`.

Multiple class names are already supported with the ability to pass any 
number of arguments.  It is defined in the spec, and I implemented it, as:

getElementsByClassName(className1 [, className2, ...] )

Lachlan Hunt

More information about the whatwg mailing list