views:

490

answers:

2

I've never had a reason to put a label element inside of a legend element (never really thought about it or seen it done). But with the design I'm implementing, it's tempting to do so.

Here's what I'm tempted to do:

<fieldset>
<legend><label for="formInfo">I would like information on</label></legend>
<select id="formInfo">
 <option value="Cats">Cats</option>
 <option value="Dogs">Dogs</option>
 <option value="Lolz">Lolz</option>
</select>
</fieldset>

It works as expected (clicking the label focuses the corresponding input) in Firefox3, Safari, Opera, and IE6/7 and it passes validation, but I'm just wondering if there are any known reasons (accessibility? semantics? browser issues) why this shouldn't be done

A: 

well, the label element itself seems fine - it's the description of the "formInfo" element, so that's no worries. Semantically, however, what's this saying about the legend element? It's supposed to be a caption for the entire fieldset....

nickf
+4  A: 

Where is your </fieldset>?

Semantically, legend describes a fieldset, just as label describes a single field.

Fieldsets are supposed to be used to group together semantically related fields (for instance, an "address" fieldset might have input fields for street, city and country).

Assuming you have more than one field in the fieldset, then doing what you suggest doesn't semantically make sense -- you need to create separate legend text that describes the fieldset, then a label for each field.

If you only have one field, then you don't need fieldset or legend at all.

So, basically, you shouldn't do what you're doing.

If you're doing it to have extra elements to attach CSS rules or Javascript events to, you're better off using generic elements like div and span that won't confuse text-to-speech and other non-visual user agents.

i.e., putting in a div or span is effectively neutral in terms of accessibility/semantics (it implies nothing) versus misusing a semantic element (even if only slightly, as in this case), which is potentially misleading. Imagine even the best-case scenario for your layout in a text-to-speech browser: The text is read aloud twice, once as legend and once as label -- why would someone want the phrase "I would like information on" read aloud twice to them? Especially as it only makes sense in the context of the choices in the select control.

joelhardi
Voted up. RE: last paragraph - ...and if there are further fields in the fieldset then the legend will be announced for those fields too, which will probably be undesirable.
Lee Kowalkowski
Closing FIELDSET tag got munched by the text editor. I went to add it back in, it was still there, but not in the code block.I appreciate your reply - just the well thought out input I was hoping for. I was on the fence and you've eloquently talked me down. Thanks!
Andy Ford
As a side note, the LEGEND element is a little difficult to control with css. I was going to use absolute positioning, but ran into problems. In the end, I decided to forgo the LEGEND element and use a LABEL with a special class.
Andy Ford
Cool, glad it was helpful ... I just wasn't sure without the </fieldset> if there were any more fields or not. I, too, find myself tempted to abuse HTML elements from time to time. :)
joelhardi