[whatwg] getElementsByClassName()

Shadow2531 shadow2531 at gmail.com
Wed Feb 15 09:55:05 PST 2006

On 2/15/06, Ric Hardacre <whatwg at cycloid.f9.co.uk> wrote:
> Shadow2531 wrote:
> >
> > I was *messing* around with 2 different *examples*.
> >
> > 1.) http://shadow2531.com/opera/js/getElementsByClassName/000.html
> >
> > That one supports:
> > getElementsByClassName(string);
> > getElementsByClassName(array);
> >
> > If the string has spaces in it, it's considered that nothing will
> > match and returns null.
> > If it's an array, all must be present for an element to match.
> >
> > 2.) http://shadow2531.com/opera/js/getElementsByClassName/001.html
> >
> > Now this one supports the same 2 types, but the string handling is
> > different. The string is space-separated.
> >
> > So, with this second example, you can do:
> >
> > document.getElementsByClassName("aaa");
> > document.getElementsByClassName(["bbb", "ccc"]);
> > document.getElementsByClassName("bbb ccc");
> >
> > (The second 2 produce the same result. The 3rd one might just be
> > cleaner in certain situations)
> >
> > I'm liking what options the second example provides. (not necessarily
> > the code as I just threw it together and didn't think about
> > exceptions, optimization and code size. Plus I just used a global
> > function for the example.)
> >
> > Do you agree with the string being space-separated?
> > It seems to make sense at least for html where a classname can't have spaces.
> looks good to me, the only quirk of this would be that you cant choose
> class="foo bar" specifically over class="foo" with gebcn("foo bar") but
> if this is the stated behaviour then i guess that's ok.

gebcn("foo bar") should match any element that has *both* "foo" and
"bar", but *not necessarily just* "foo" and "bar".

If the element doesn't have both foo and bar, the element doesn't
match. Therefore, gebcn("foo bar") wouldn't match class="foo" because
"bar" is not present. None of the methods of searching in my examples
will work as a getElementsByClassAttributeValue. I don't think gebcn
is supposed to do that anyway.

> on a side note, it might also be worth stating that in this case
> gebcn("foo bar") should only return the class="foo bar" element once,
> not twice (one instance for "foo" and one instance for "bar"). just for
> completeness

Correct, each element should only be evaluated once and if just one
class you search for in the element's class attribute is not present,
the element does not match and it moves on to the next element.

That was the idea at least., but maybe my examples don't fully do what
I meant or were you just clarifying? :)


More information about the whatwg mailing list