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

[whatwg] getElementsByClassName()

From: Shadow2531 <shadow2531@gmail.com>
Date: Fri, 3 Feb 2006 07:19:38 -0500
Message-ID: <6b9c91b20602030419x230aa6f9o5e3f7363b564454b@mail.gmail.com>
On 2/3/06, Gervase Markham <gerv at mozilla.org> wrote:
> 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.

O.K. agreed.

> > 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).

Point taken.  That would be fine.  I think I now agree with you about
not normalizing an argument with spaces.

> > 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.

O.K.  Then, it should be getElementByClassName*s*() where you have
have 1 or more classname arguments.  If you pass more than 1 class
name, both class names have to be present in the classname attribute
for the element to match.

I'm just being picky here though, but getElementByClassName() implies
only one classname where as getElementByClassNames() implies one or
more. Although as long as you know what the function does, that hardly
matters, but I bring it up anyway. :)

burnout426
Received on Friday, 3 February 2006 04:19:38 UTC

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