[whatwg] getElementsByClassName()

On 2/3/06, Gervase Markham <gerv at mozilla.org> wrote:
> Shadow2531 wrote:
> > 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.
> 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 was *messing* around with 2 different *examples*.

1.) http://shadow2531.com/opera/js/getElementsByClassName/000.html

That one supports:

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(["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.


Received on Tuesday, 14 February 2006 08:48:10 UTC