Testing hints and errors on checkbox groups
Hints and error messages for a group of checkboxes need to be programmatically associated with the group, not just placed near it. Hints and errors use the same method, so they are tested together. Every example is pre-set in the markup, so no JavaScript is needed.
Simplest approach: link the hint or error to the group with aria-describedby. See Recommended practice.
Every example has a coloured box showing how to read it:
Poor practice: allowed, but not recommended.
Acceptable: allowed, and does part or all of the job. The box says what it does and does not give you.
Control: a baseline.
1. Controls
Baselines with no hint or error shown.
Example 1.1: fieldset
Control: A group with no hint or error shown.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<div>
<input type="checkbox" id="c1-dog" name="c1" value="dog">
<label for="c1-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c1-cat" name="c1" value="cat">
<label for="c1-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c1-rabbit" name="c1" value="rabbit">
<label for="c1-rabbit">Rabbit</label>
</div>
</fieldset>
Example 1.2: div with role="group"
Control: A group with no hint or error shown.
HTML markup
<div role="group" aria-labelledby="c2-label">
<h4 id="c2-label">Which pets do you have?</h4>
<div>
<input type="checkbox" id="c2-dog" name="c2" value="dog">
<label for="c2-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c2-cat" name="c2" value="cat">
<label for="c2-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c2-rabbit" name="c2" value="rabbit">
<label for="c2-rabbit">Rabbit</label>
</div>
</div>
2. Text with no association
The hint or error is visible, but nothing links it to the group.
None of these options is good practice.
Example 2.1: Hint, not linked
Poor practice: The hint is in a paragraph inside the group. Nothing associates it with the group, so assistive technology users may not hear it.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p>Choose at least one pet.</p>
<div>
<input type="checkbox" id="c3-dog" name="c3" value="dog">
<label for="c3-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c3-cat" name="c3" value="cat">
<label for="c3-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c3-rabbit" name="c3" value="rabbit">
<label for="c3-rabbit">Rabbit</label>
</div>
</fieldset>
Example 2.2: Error, not linked
Poor practice: The error is in a paragraph inside the group. Nothing associates it with the group, so assistive technology users may not hear it.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c4-dog" name="c4" value="dog">
<label for="c4-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c4-cat" name="c4" value="cat">
<label for="c4-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c4-rabbit" name="c4" value="rabbit">
<label for="c4-rabbit">Rabbit</label>
</div>
</fieldset>
3. Error as text in the group's name
The error message is written into the group's name: the legend, or the labelling element.
None of these options is good practice.
Example 3.1: fieldset
Poor practice: The error is part of the group's accessible name, so it is announced when users enter the group. But it makes the name longer and changes it whenever there is an error. Prefer aria-describedby.
HTML markup
<fieldset>
<legend>Which pets do you have? <span class="error">Error: Choose at least one pet.</span></legend>
<div>
<input type="checkbox" id="c5-dog" name="c5" value="dog">
<label for="c5-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c5-cat" name="c5" value="cat">
<label for="c5-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c5-rabbit" name="c5" value="rabbit">
<label for="c5-rabbit">Rabbit</label>
</div>
</fieldset>
Example 3.2: div with role="group"
Poor practice: The error is part of the group's accessible name, so it is announced when users enter the group. But it makes the name longer and changes it whenever there is an error. Prefer aria-describedby.
HTML markup
<div role="group" aria-labelledby="c6-label">
<h4 id="c6-label">Which pets do you have? <span class="error">Error: Choose at least one pet.</span></h4>
<div>
<input type="checkbox" id="c6-dog" name="c6" value="dog">
<label for="c6-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c6-cat" name="c6" value="cat">
<label for="c6-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c6-rabbit" name="c6" value="rabbit">
<label for="c6-rabbit">Rabbit</label>
</div>
</div>
4. Hint linked to the group
The hint is linked to the group with aria-describedby.
Example 4.1: fieldset
Acceptable: The hint is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.
HTML markup
<fieldset aria-describedby="c7-hint">
<legend>Which pets do you have?</legend>
<p id="c7-hint">Choose at least one pet.</p>
<div>
<input type="checkbox" id="c7-dog" name="c7" value="dog">
<label for="c7-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c7-cat" name="c7" value="cat">
<label for="c7-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c7-rabbit" name="c7" value="rabbit">
<label for="c7-rabbit">Rabbit</label>
</div>
</fieldset>
Example 4.2: div with role="group"
Acceptable: The hint is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.
HTML markup
<div role="group" aria-labelledby="c8-label" aria-describedby="c8-hint">
<h4 id="c8-label">Which pets do you have?</h4>
<p id="c8-hint">Choose at least one pet.</p>
<div>
<input type="checkbox" id="c8-dog" name="c8" value="dog">
<label for="c8-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c8-cat" name="c8" value="cat">
<label for="c8-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c8-rabbit" name="c8" value="rabbit">
<label for="c8-rabbit">Rabbit</label>
</div>
</div>
5. Error linked to the group
The error is linked to the group with aria-describedby.
Example 5.1: fieldset
Acceptable: The error is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.
HTML markup
<fieldset aria-describedby="c9-error">
<legend>Which pets do you have?</legend>
<p class="error" id="c9-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c9-dog" name="c9" value="dog">
<label for="c9-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c9-cat" name="c9" value="cat">
<label for="c9-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c9-rabbit" name="c9" value="rabbit">
<label for="c9-rabbit">Rabbit</label>
</div>
</fieldset>
Example 5.2: div with role="group"
Acceptable: The error is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.
HTML markup
<div role="group" aria-labelledby="c10-label" aria-describedby="c10-error">
<h4 id="c10-label">Which pets do you have?</h4>
<p class="error" id="c10-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c10-dog" name="c10" value="dog">
<label for="c10-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c10-cat" name="c10" value="cat">
<label for="c10-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c10-rabbit" name="c10" value="rabbit">
<label for="c10-rabbit">Rabbit</label>
</div>
</div>
6. Hint and error linked to the group
One aria-describedby lists both IDs, separated by a space.
Example 6.1: fieldset
Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.
HTML markup
<fieldset aria-describedby="c11-hint c11-error">
<legend>Which pets do you have?</legend>
<p id="c11-hint">Choose at least one pet.</p>
<p class="error" id="c11-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c11-dog" name="c11" value="dog">
<label for="c11-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c11-cat" name="c11" value="cat">
<label for="c11-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c11-rabbit" name="c11" value="rabbit">
<label for="c11-rabbit">Rabbit</label>
</div>
</fieldset>
Example 6.2: div with role="group"
Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.
HTML markup
<div role="group" aria-labelledby="c12-label" aria-describedby="c12-hint c12-error">
<h4 id="c12-label">Which pets do you have?</h4>
<p id="c12-hint">Choose at least one pet.</p>
<p class="error" id="c12-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c12-dog" name="c12" value="dog">
<label for="c12-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c12-cat" name="c12" value="cat">
<label for="c12-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c12-rabbit" name="c12" value="rabbit">
<label for="c12-rabbit">Rabbit</label>
</div>
</div>
7. aria-describedby on the checkboxes
The hint or error is linked to the checkboxes themselves.
None of these options is good practice.
Example 7.1: aria-describedby on each checkbox
Poor practice: The error is repeated on every checkbox, and it is not associated with the group itself.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error" id="c13-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c13-dog" name="c13" value="dog" aria-describedby="c13-error">
<label for="c13-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c13-cat" name="c13" value="cat" aria-describedby="c13-error">
<label for="c13-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c13-rabbit" name="c13" value="rabbit" aria-describedby="c13-error">
<label for="c13-rabbit">Rabbit</label>
</div>
</fieldset>
Example 7.2: aria-describedby on one checkbox only
Poor practice: Only that checkbox has the error description, so users may think only that checkbox is in error, not the group.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error" id="c14-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c14-dog" name="c14" value="dog" aria-describedby="c14-error">
<label for="c14-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c14-cat" name="c14" value="cat">
<label for="c14-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c14-rabbit" name="c14" value="rabbit">
<label for="c14-rabbit">Rabbit</label>
</div>
</fieldset>
8. Recommended practice
The same examples as section 6, repeated here so they are easy to find.
Example 8.1: fieldset
Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.
HTML markup
<fieldset aria-describedby="c15-hint c15-error">
<legend>Which pets do you have?</legend>
<p id="c15-hint">Choose at least one pet.</p>
<p class="error" id="c15-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c15-dog" name="c15" value="dog">
<label for="c15-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c15-cat" name="c15" value="cat">
<label for="c15-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c15-rabbit" name="c15" value="rabbit">
<label for="c15-rabbit">Rabbit</label>
</div>
</fieldset>
Example 8.2: div with role="group"
Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.
HTML markup
<div role="group" aria-labelledby="c16-label" aria-describedby="c16-hint c16-error">
<h4 id="c16-label">Which pets do you have?</h4>
<p id="c16-hint">Choose at least one pet.</p>
<p class="error" id="c16-error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c16-dog" name="c16" value="dog">
<label for="c16-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c16-cat" name="c16" value="cat">
<label for="c16-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c16-rabbit" name="c16" value="rabbit">
<label for="c16-rabbit">Rabbit</label>
</div>
</div>