[whatwg] getElementsByClassName()

Gervase Markham gerv at mozilla.org
Fri Feb 3 02:39:30 PST 2006


Shadow2531 wrote:
> I just threw them together as a proof of concept, but I have no doubt
> that the class attribute value should be a space separated list of
> classnames and the getElementByClassName function should split up the
> class attribute value into an array and then search for the class name
> to see if there's a match (Or use regex to be simpler). I also added a
> case insensitve search option.

IMO there should be no option; if class names are defined as
case-insensitive, we should search case-insensitively, and if they
aren't, we shouldn't.

> As the case above, getElementByClassName("foo bar") doesn't make sense
> to me because there are 2 class names in there and it's
> getElementByClassName, not getElementsByClassName*s*().
> 
> In getElementByClassName("foo bar") , "foo bar" should be normalized
> "foo" and therefore match all 3 because they all have "foo" in the
> class attribute value.

I think this behaviour would violate the rule of least surprise. Better
behaviour would be to match nothing, treating "foo bar" as a single
class name. Rationale: just because HTML doesn't allow class names with
spaces doesn't mean no other languages do, and the coding error would be
much easier to spot (you'd get no elements at all).

> getElementsByClassName*s*() should be a separate function and I agree,
> getElementsByClassNames*s*("foo", "bar") would be great if possible.

I also think it's not ideal to have two functions with such similar names.

Gerv



More information about the whatwg mailing list