[Javascript] javascript and id's

Paul Novitski paul at novitskisoftware.com
Tue Oct 4 12:47:59 CDT 2005


At 10:22 AM 10/4/2005, Anthony Ettinger wrote:
>Well, from what I've talked with some co-workers, they
>are saying 'id' should be used instead of 'name'.
>Although I'm sure they will always be backwards
>compatible. The problem I raised was what do you do in
>a radio group? where id would/should be the name (but
>id implies only one per page).
>
>I don't know who is right/wrong. But the latest
>javascript I've seen all seems to revolve around
>getElementById.


The NAME attribute isn't unique -- there can be more than one element 
with the same name on the page.  This is exactly how radio buttons 
work -- they all share the same name, and only the selected element's 
value is transmitted on submit.  If more than one active field has 
the same name in a form, their values are submitted as a comma-delimited list.

As I understand it, the NAME attribute today is used to organize form 
fields for submission but isn't otherwise useful in the DOM.

In the DOM, the two methods we're given to locate items immediately 
are getElementById() and getElementsByTagName(), but 
"getElementsByName()" doesn't exist.

See the HTML 4.01 spec:
http://www.w3.org/TR/html4/interact/forms.html#adef-name-FORM
"name = cdata [CI]
     "This attribute names the element so that it may be referred to 
from style sheets or scripts. Note. This attribute has been included 
for backwards compatibility. Applications should use the id attribute 
to identify elements."

It's always a good idea to run your HTML through the W3C validator 
http://validator.w3.org/ -- it will point out if & where your markup 
strays from the recommended path.

Paul 




More information about the Javascript mailing list