Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the HTML 'label' tag with radio buttons

Does the label tag work with radio buttons? If so, how do you use it? I have a form that displays like this:

First Name: (text field) Hair Color: (color drop-down) Description: (text area) Salutation: (radio buttons for Mr., Mrs., Miss) 

I'd like to use the label tag for each label in the left column to define its connection to the appropriate control in the right column. But If I use a radio button, the spec seems to indicate that suddenly the actual "Salutation" label for the form control no longer belongs in the label tag, but rather the options "Mr., Mrs., etc." go in the label tag.

I've always been a fan of accessibility and the semantic web, but this design doesn't make sense to me. The label tag explicitly declares labels. The option tag selection options. How do you declare a label on the actual label for a set of radio buttons?

UPDATE: Here is an example with code:

<tr><th><label for"sc">Status:</label></th>     <td>&#160;</td>     <td><select name="statusCode" id="sc">             <option value="ON_TIME">On Time</option>             <option value="LATE">Late</option>         </select></td></tr> 

This works great. But unlike other form controls, radio buttons have a separate field for each value:

<tr><th align="right"><label for="???">Activity:</label></th>     <td>&#160;</td>     <td align="left"><input type="radio" name="es" value="" id="es0" /> Active &#160;         <input type="radio" name="es" value="ON_TIME" checked="checked" id="es1" /> Completed on Time &#160;         <input type="radio" name="es" value="LATE" id="es2" /> Completed Late &#160;         <input type="radio" name="es" value="CANCELED" id="es3" /> Canceled</td> </tr> 

What to do?

like image 811
GlenPeterson Avatar asked Nov 07 '12 16:11

GlenPeterson


People also ask

What is the HTML tag name for the radio button?

The <input type="radio"> defines a radio button. Radio buttons are normally presented in radio groups (a collection of radio buttons describing a set of related options).

How do you label a group of radio buttons?

To label a group of fields (e.g. several radio buttons that share the same name ), you use a <fieldset> tag with a <legend> element.

Do radio buttons need labels?

Each radio button must be accompanied by a label describing the choice it represents. Another alternative is to provide an ID for each radio element and then use that ID in the for attribute of the label.


1 Answers

Does the label tag work with radio buttons?

Yes

If so, how do you use it?

Same way as for any other form control.

You either give it a for attribute that matches the id of the control, or you put the control inside the label element.

I'd like to use the label tag for each label in the left column

A label is for a control, not a set of controls.

If you want to caption a set of controls, use a <fieldset> with a <legend> (and give a <label> to each control in the set).

<fieldset>   <legend> Salutation </legend>   <label> <input type="radio" name="salutation" value="Mr."> Mr. </label>   <label> <input type="radio" name="salutation" value="Mrs."> Mrs. </label>   <!-- etc --> </fieldset> 
like image 50
Quentin Avatar answered Sep 17 '22 18:09

Quentin