views:

583

answers:

5

So.. I have been trying to avoid radio buttons at all cost in my web development projects because I just cant figure out the right way to code them.

Do you set a label for radio button.. if so, how does that work for screen readers.

The way I have been doing most of my forms is by using un-ordered lists. Each input being a list item. I just always run into trouble when doing radio buttons. I can never get it to look right across all browsers..

For example, look at http://usfultimate.com/index.php/hatter/register . Does this structure make sense?

Quick structure sample:

<ul>
  <li>
    <label for='first_name'>First Name:</label>
    <input type="text" name="first_name" value=""  />
  </li>
  <li>
    <label for='last_name'>Last Name:</label>
    <input type="text" name="last_name" value=""  />
  </li>
  <li>
    <label for='email'>Email Address:</label>

    <input type="text" name="email" value=""  />
  </li>

  <li class="radio">
    <ul>
      <li>
        <input type="radio" name="skill" value="Never Played" id="neverPlayed" />
        <label for="neverPlayed">Never Played</label>
      </li>

      <li>
        <input type="radio" name="skill" value="Unorganized Pickup"
          id="unorganizedPickup" />
        <label for="unorganizedPickup">Unorganized Pickup</label>
      </li>
      <li>
        <input type="radio" name="skill" value="Organized Pickup / League Play"
          id="organizedPickup" />
        <label for="organizedPickup">Organized Pickup or League Play</label>
      </li>
      <li>
        <input type="radio" name="skill" value="Played on a Competative Team"
          id="competativeTeam" />
        <label for="competativeTeam">Competative Team</label>
      </li>

      <li>
        <input type="radio" name="skill" value="Baller Shot Caller" id="baller" />
        <label for="baller">Baller Shot Caller</label>
      </li>
    </ul>
  </li>
</ul>

Any pointer would be appreciated!

+2  A: 
<label for="baller">Baller Shot Caller</label>
<input type="radio" name="skill" value="Baller Shot Caller" id="baller" />

As for screen readers, it depends but its just text.

Daniel A. White
i put the label after the radio field so it would look better.. the only change i see here is that you reversed it. so im doing it the right way.. just reverse label?
Roeland
+1 the for in the label matches the id in the input. lets you click on the text in all browsers. The variable that comes back is skill and has a value depending on the radio button. This is the right way to do radio buttons.
Byron Whitlock
Also the text is usually after the radio button.
Byron Whitlock
so if this is the correct way of doing it, im having a world of trouble styling it right cross browsers. i float radio button and label to left and it looks ok. problem is i cant add padding to radio button.
Roeland
A: 

You can also organize your screen using the 'fieldset' tag. It will group nicely the different sets of radio buttons.

Anax
+1  A: 

You can use the for attribute to explicitly associate a label with a form element like a checkbox or radio button as you have shown in the example.

Also, in HTML 4.01 you can also implicitly associate a label with a form element by doing the following (example from linked w3c page):

<FORM action="..." method="post">
<P>
<LABEL>
   First Name
   <INPUT type="text" name="firstname">
</LABEL>
<LABEL>
   <INPUT type="text" name="lastname">
   Last Name
</LABEL>
</P>
</FORM>

This is sometimes more convenient, but not always appropriate such as in scenarios where your label's are not adjacent to the form elements in the HTML (e.g.: in a table).

Justin Johnson
+9  A: 

For accessibility:

  1. Organize your radio buttons as a fieldset and give it a legend attribute, so that screen-readers understand what each group of radio buttons means.
  2. Give each radio button an ID, and use the ID in the for="" attribute of an associated label element. This isn't just good for accessibility - it also means you can select a radio button by clicking the label (which is much bigger and easier to hit than the radio button itself)

Then, use CSS to style your fieldset, legend and label attributes to match your site design, as in this example:

<html>
 <head>
  <style>
  body {
   font-family: arial;
   font-size: 15px;
   line-height: 1.4em;
  }
  fieldset.radioGroup  { border: none; }
  fieldset.radioGroup  legend { font-weight: bold; }

  /* Make each radio/label render on a new line */
  fieldset.radioGroup  label { display: block; }

  /* add some horizontal spacing between radio buttons and their label text */
  fieldset.radioGroup label input { margin-right: 32px; }
  </style>
 </head>
<body>
 <fieldset class="radioGroup">
  <legend>Skill Level</legend>
  <label for="neverPlayedRadioButton">
   <input type="radio" name="skill" value="neverPlayed" id="neverPlayedRadioButton" />
   Never Played
  </label>
  <label for="unorganizedPickupRadioButton">
    <input type="radio" name="skill" value="unorganizedPickup" id="unorganizedPickupRadioButton" />
    Unorganized Pickup
  </label>
  <label for="organizedPickupRadioButton">
   <input type="radio" name="skill" value="organizedPickup" id="organizedPickupRadioButton" />
   Organized Pickup / League Play
  </label>
 </fieldset>
</body>
</html>
Dylan Beattie
is it ok to put fieldsets inside of fieldsets? because i would want to use fieldsets for larger groups of inputs.. there may be 2 text inputs and 2 radio input areas.. all in 1 fieldset. so then i would have a fieldset inside of a fieldset.
Roeland
Yes - according to the W3C validator, nested fieldsets are valid markup in both HTML 4.01 Transitional and XHTML 1.0.
Dylan Beattie
Great answer, Dylan! I always forget exactly how to connect up the labels and the buttons so that clicking on labels works, and now I can just always come here.
Brandon Craig Rhodes
+2  A: 

Your code is incorrect in that your input tags are inside your label elements. Ideally labels should only contain text. Otherwise your code is entirely correct.

Some things to keep in mind is that users who are using screen readers are unable to establish a common context for a series of radio buttons and a bit of text that established that common context. Others have pointed out putting your radio buttons in a fieldset and using the legend element to establish this context. The ideal solution for accessibility, however, is to ensure that the text value and label are clear enough to be understood completely on their own. From your example a blind user will likely have trouble knowing that "unorganized pickup" is a choice in relation to a skill level. It would be more accessible to rephrase that to say something like "unorganized pickup skill level". Then there is absolutely no confusion on what the radio button is about.

You have used an unordered list to structure your radio buttons. That is fine. You could also use paragraphs grouped under a fieldset or div container. What is important is how you wish the structure of those elements to be conveyed. An unordered list suggest a series of something where the enumeration of that series is not relevant, but elements of the series are necessary to represent the value of the series as a whole. A group of paragraphs suggest a formal declaration of content where each paragraph is intended for explicit consumption without immediate regard to other similar elements in the group, the order of such elements, or the quantity of such elements so long as each paragraph is individually consumed. Semantic structure is important, but what is more important is what you wish such a structure to communicate to assisting technologies that have to analyze and parse your code.

Your code matches the more strict requirements of XHTML. Good. Do not deviate from uniform strictness towards flexible incompetence merely because such incompetence is commonly allowed.

wait, im confused.. in my code, the input tags are outside of my label elements, are you referring to the code that Justin posted? The only problem with having the label be so descriptive is that it creates repetition (word skill level would be repeated over and over.) Wouldnt the screen reader associate the legend to the group of radio buttons if I seperate them using fieldsets with legends? I guess the other things I could do is put the "skill level" portion in a span that is hidden from normal browsers.. but still works in screen readers.
Roeland
You are right. I was probably looking at the code posted by Dylan Beattie. I agree that detailed specification can sound repetitive. Still its necessary to better assist users who are using screen readers. You could also juice up the label text so that it is not exactly repetitive. A screen reader will announce the legend and will announce that the radio buttons are in the fieldset, but will not explicitly announce any further relationships. That is a limitation of the SGML form of HTML. Putting more specific text that is hidden from browsers is a strongly suggested accessibility trick.
Just don't hide the text using inline styles. Screen readers are capable of reading display:none and visibility:hidden when it is written inline. They do that to comply with JavaScript that can hide and show text in the same manner a visual user would experience.
The following is emphatically NOT "invalid": `<label><input type="radio" name="N1" value="Y">Yes</label>` - indeed, it's one of the accepted ways of associating labels with buttons.
Martha
That would actually be semantically invalid, because you are informing the user the input is an element of the label instead of associating the label to the input through virtue of the IDREF data type.