Obtaining same named elements from the HTMLFormElement.elements collection

Stanimir Stamenkov <[email protected]> Tue, 13 Jul 2010 19:22:14 +0300
Newsgroups gmane.comp.mozilla.devel.dom
Message-ID <[email protected]>
I have a form in HTML document containing the following elements:

   <input type="checkbox" name="stuff" value="1">
   <input type="checkbox" name="stuff" value="2">
   <input type="checkbox" name="stuff" value="3">

If 'form' is reference to the form, trying:

   window.alert(form.elements.namedItem('stuff'));

yields "[object HTMLInputElement]", while using:

   window.alert(form.elements['stuff']);

yields "[object NodeList]".  Using Safari 5/Chrome 5 both forms 
yield "[object NodeList]".  Using Opera 10.60/IE8 both forms yield 
"[object HTMLCollection]".  Which one is right?

I've given HTMLCollection.namedItem(name) a try as accessing form 
elements with name of "item" like |form.elements['item']| gives me 
the function item() of the HTMLCollection, instead of the needed 
elements.  This however brought the above discrepancy and I'm not 
sure I can safely use cross-browser |form.elements['field-name']| to 
obtain a collection with all form elements with name of 
"field-name", anymore.

-- 
Stanimir

... An SQL statement walks into a bar and sees two tables. It 
approaches, and asks “may I join you?”