Re: name, id, and forms

by Andrew McFarland <aamcf(at)aamcf.co.uk>

 Date:  Tue, 08 Oct 2002 20:01:43 +0100
 To:  hwg-techniques(at)hwg.org
 In-Reply-To:  1
  todo: View Thread, Original
At 13:53 08/10/02 -0400, Greg Hart wrote:
>I've read repeatedly that in converting from HTML to XHTML, you must at 
>least replace "name" with "id", or to keep older browsers happy, have both 
>present.

This is half right.

In HTML 4.01, a, applet, form, frame, iframe, img, and map all have the 
name attribute defined. For these elements in XHTML 1.0 you should use id 
as well as / instead of the name attribute.

Checkboxes and radio buttons are different. The name attribute doesn't 
apply to the individual elements, but their group, so for checkboxes and 
radio buttons you should use the name attribute as before. Of course, you 
can also give each radio button an id if you like, but it isn't essential.

Here is an HTML 4.01 example:

<.input type="checkbox" name="color" value="blue" id="box1">
<.input type="checkbox" name="color" value="red" id="box2">

Note the two checkboxes, which are part of the same group, have the _same_ 
name but different ids.

In XHTML 1.0 the above would be:

<.input type="checkbox" name="color" value="blue" id="box1" />
<.input type="checkbox" name="color" value="red" id="box2" />

The two checkboxes _still_ have the _same_ name but different ids.

>Also, what browsers will be a problem and won't see the "id", requiring 
>the "name" be left in to process the form?

For form elements, the name attribute should be used in the same way it was 
in HTML 4.01.

Generally only very few browsers will require the name attribute to be 
present in other elements for them to work properly. Netscape 4 probably 
does, but if you are coding with Netscape 4 in mind that is the least of 
your worries.

Andrew

--
http://aamcf.co.uk/

HWG hwg-techniques mailing list archives, maintained by Webmasters @ IWA