form elements

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi Gurus

I have been making forms for a while now, but something that I never really
got sorted was the difference between name and ID.

My question is:  "for each form element (e.g. input, textarea, select),
should I use an ID and a Name and if so, should I use the same name for both
of them (e.g. ID=test Name=test).

What does Javascript use most of the time (ID or Name or either) and what
other considerations should I make?


- Nicolaas

Re: form elements

Quoted text here. Click to load it

Only the name is significant for a form control. It's paired with the  
value for that control and sent to the server.

An id on a form control is (generally) the same as an id on any other  
element. The only special case is when used in conjunction with the for  
attribute of a LABEL element. I'd reserve ids for when you need to  
uniquely identify an element for use with a stylesheet or possibly a  
script (see below).

Quoted text here. Click to load it

A client-side script could use either. The only reason I can think of at  
the moment for using an id, rather than a name, to access a form control  
is if it shares its name with other controls (for example, a group of  
radio buttons) and you want to conveniently access it. However, in most  
cases that's excessive as you could obtain a collection of elements for  
that group and access the specific element by ordinal.

That said, when using a string with the elements collection of a FORM  
element, the user agent will prioritise a matching id over a matching name.

Quoted text here. Click to load it

None that I can think of, unless you want to support NN4 (in which case  
don't rely on id attributes).


Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Re: form elements

Quoted text here. Click to load it

Thank you so much Michael.  That confirms my suspicions and explains what I
did not know.  Very useful!

Site Timeline