[whatwg] getElementsByClassName()

Ric Hardacre whatwg at cycloid.f9.co.uk
Wed Feb 15 05:03:59 PST 2006



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.

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

ric



More information about the whatwg mailing list