Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- form elements
January 9, 2005, 9:59 pm
rate this thread
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).
other considerations should I make?
Re: form elements
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).
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.
None that I can think of, unless you want to support NN4 (in which case
don't rely on id attributes).
Replace ".invalid" with ".uk" to reply by e-mail.