Simulated checkboxes are said to be “simulated” because they do not use the default form elements in HTML, namely the <input type=”checkbox”/>
fields. They are often used because they are more visually attractive.
The basic HTML code uses standard checkboxes:
<form action="index.html" method="post"> <h2 id="question">Which sports do you play?</h2> <ul id="answers"> <li class="choice"> <label for="golf"> <input type="checkbox" name="golf" id="golf" /> Golf </label> </li> <li class="choice"> <label for="polo"> <input type="checkbox" name="polo" id="polo" /> Polo </label> </li> <li class="choice"> <label for="tennis"> <input type="checkbox" name="tennis" id="tennis" /> Tennis </label> </li> </ul> <input type="submit" value="Submit information" /> </form>
Without JavaScript, a standard form is provided to the user.
The scripts must handle the following points:
<label>
elements and their content are replaced by the text that was initially within the <input type=“checkbox” />
tags.aria-hidden=“true”
attribute is applied to the image to hide it from users with screen readers.alt
attribute associated with the image describes the status of the checkbox; for example alt=“Unchecked: ”
.role=“group”
attribute is applied to the container of the checkboxes. In the example, the <ul> tag is used.role=”checkbox”
attribute is applied to each of the checkboxes. In the example, the <li>
tag is used.aria-checked=“false”
attribute is applied to each of the checkboxes by default. This attribute value then changes dynamically to true
when the checkbox is selected and false
when it is not.space
key when the focus is set on the checkbox.tabindex=“0”
is applied to each of the simulated checkboxes so that they can receive the focus. aria-labelledby
attribute is applied to the container of the checkboxes. The value of this attribute is retrieved from the id
associated with the question, instruction, etc.The resulting code in JavaScript is as follows:
<form method="post" action="index.html"> <h2 id="question">Which sports do you play?</h2> <ul id="answers" role="group" aria-labelledby="question"> <li class="choice" role="checkbox" aria-checked="false" tabindex="0"> <img aria-hidden="true" src="images/checkbox-empty.png" alt="unchecked: "> Golf </li> <li class="choice" role="checkbox" aria-checked="false" tabindex="0"> <img aria-hidden="true" src="images/checkbox-empty.png" alt="Unchecked : "> Polo </li> <li class="choice" role="checkbox" aria-checked="false" tabindex="0"> <img aria-hidden="true" src="images/checkbox-empty.png" alt="Unchecked : "> Tennis </li> </ul> <input type="submit" value="Submit information"> </form>