[whatwg] getElementsByClassName()

Dean Edwards wrote:
> Jim Ley wrote:
>> That is not immediately apparent, and neither is it apparent that a
>> classname specific shortname is worthwhile when a CSSSelector one
>> would be more appropriate.    You don't continually add methods,
>> methods are complexity, they need writing, they need testing etc.  you
>> have to have a reason to add a method.
>>
> 
> A good API should expose a meaningful interface to its consumer. If we 
> are allowed to define data according to a skew on its structure (HTML 
> className) then the interface to that data should reflect this.
> 
> The getElementsByClassName() method has already been compared to the 
> getElementsByTagName() and getElementById() methods. "class" is another 
> way to slice the data. It is a core part of HTML, the language we are 
> extending.
> 
> To me, getElementsByClassName() is a missing feature of the DOM to 
> support HTML4. I'd be happy if that were fixed in HTML5.
> 

I think this is a very clear explanation of the reasons to include it, 
it's not as if we're discussing GetElementsByOnMouseOver() here. But 
let's throw out a use case off the top of my head:

i'm making a shopping cart and each product has a checkbox next to it, 
to select it for deletion, there are also other checkboxes on the page, 
one to agree to the terms, another to sign up for the mailing list, 
another to sign up for "advertisements from our business partners", 
another one up at the top right by a search box for a search option and 
finally the "select all for deletion" checkbox. when i click on the 
latter i want to check all the individual checkboxes for each product. 
as they're in the basket i'm specifically styling them class="prodchck". 
so to check them all all i need to do is:

function checkall()
{
	var a = document.getElementsByClassName("prodchck");
	for( var i = 0 ; i < a.length ; i++ )
		a[i].checked = true;
}

Received on Tuesday, 7 February 2006 01:46:06 UTC