[whatwg] getElementsByClassName()

ROBO Design robodesign at gmail.com
Sat Feb 4 03:06:25 PST 2006


On Sat, 04 Feb 2006 03:49:23 +0200, Brad Fults <bfults at gmail.com> wrote:

> On 2/3/06, Jim Ley <jim.ley at gmail.com> wrote:
<...>
>
> I can't believe that you're so insistent upon this extremely narrow
> set of use cases and that there aren't any other popular use cases for
> getElementsByClassName().
>
> If there are no use cases for this function then what are the use
> cases for getElementById()? I suppose this should be
> addEventToElementById()? How about getElementsByTagName()? That one
> too, eh?
>
> The point of getElementsByClassName() is superior control over the
> DOM. Where getElementById() falls short by only returning one element
> and getElementsByTagName() falls short by only returning one,
> document-mandated type of element, getElementsByClassName() gives the
> author the control to collect arbitrary sets of elements which all
> share the same class or set of classes.
>
<...>
>
> Completely irrelevant. See getElementById() or getElementsByTagName().
> The requirement for a loaded document is to be expected when one
> wishes to manipulate the constructed DOM from that document.
>
<...>

Hello!
Very good point. There are many use-cases for getElementsByClassNames().  
This is not a useless function, it's a very useful one.

For example:

Lets take the IMDB site. Comments are marked as being positive or  
negative, so users can pick which type of comments they want to see. Each  
comment *could* (it is not currently AFAIK) styled accordingly (like  
change the background-color for negative comments, or whatever). It is  
fair to assume that the negative and positive comments will have their own  
class name. With getElementsByClassNames() you can have a UserJS that  
manipulates those comments (for example, it could parse them all and save  
a local db of negative comments :) ).

Or ... another use-case would be a site-wide JavaScript for my site that  
finds all emoticons from comments (yes, each emoticon has  
class="emoticon"). I could do "cool" things with 'em using  
getElementsByClassNames(). If I currently want to manipulate all the  
emoticons from JS I have to use getElementsByTagName('img') then check for  
.className="emoticon" which is slow and not very efficient.

Now ... we could go on and provide many more imaginary use-cases ... or we  
can go on and discuss something else :).


-- 
http://www.robodesign.ro
ROBO Design - We bring you the future



More information about the whatwg mailing list