[whatwg] getElementsByClassName()

Michel Fortin michel.fortin at michelf.com
Fri Feb 3 08:14:44 PST 2006


Le 2006-02-03 à 09:30, Jim Ley a écrit :

> Yes, but they're all using it to attach events to every one of the
> class, which is why you have to look at use cases, the reason they're
> doing it is not because getElementsByClassName is missing, but because
> addEventListenerToClass or -moz-binding etc. are missing.
>
> It's the classic mistake of looking at making the workarounds easier,
> when you should be looking at making the underlying use easier.

I have been following this list for sometime and did not have much to  
say until now. But I have a use case for this function which cannot  
work by simply using addEventListenerToClass. And that's a real  
working use case for a web application I'm working on, nothing  
hypothetical.

I have a some markup like this:

     <ul>
         <li><a href="#tab1">Tab 1</a></li>
         <li><a href="#tab2">Tab 2</a></li>
     </ul>

     <div id="tab1" class="pane">
         ...
     </div>

     <div id="tab2" class="pane">
         ...
     </div>

With the help of a style sheet, the script transforms this into some  
sort of tab view. It search for each div of class "pane". It looks  
for links pointing to each of these div elements and attach an event  
to each so that when you click on the link it shows the pane (and  
hides other panes).

  - - -

So to generalize the use case, when I want to attach an event to a  
child element or an element linked by any other mean to the element  
having that class, I can't use addEventListenerToClass.

Now, with that particular example in mind, something like  
getElementsByHref('tab1') could be useful too, but surely you can't  
have a function for each html attribute.


Michel Fortin
michel.fortin at michelf.com
http://www.michelf.com/





More information about the whatwg mailing list