[whatwg] getElementsByAttr

Sander html5 at zoid.nl
Fri Jul 6 14:38:37 PDT 2007

Dan Dorman schreef:
> On 7/6/07, Sander <html5 at zoid.nl> wrote:
>> I haven't read the whole draft yet so maybe it's in there, but can you,
>> or anyone else, explain why there is both a selectElement and a
>> selectAllElements method?
> I'm no authority, but:
> If you know you're after one element, you get to deal with it directly
> rather than first referencing the only contents of a single-element
> array.
But document.selectAllElements("#first")[0] would do the trick.

> That's not a big deal, I suppose, but a some benefits are
> reduced and more readable code (i.e., reading through the code would
> make it obvious that only a single element was desired in any given
> case).
True, but you can also make this obvious by choosing descriptive names 
for your variables.

> Also, it sort of provides a correlation with getElementById,
> and while I find the symmetry somewhat appealing, I'm not sure if that
> motivated anybody's thinking.
But there neither are singular versions of getElementsByTagName and 

If this is really the only reason and referencing the 0 index is not an 
option for the specs (maybe because in other languages than JavaScript 
calling the first item from an empty list gives an error? I just don't 
know) then I'd prefer an optional extra argument.


More information about the whatwg mailing list