Table of Contents

2.3.2. Simulated radio buttons

Purpose

“Simulated” radio buttons do not use the standard HTML form elements for radio buttons, namely the <input type=“radio” /> fields. Simulated radio buttons are often used because they are more visually attractive.

Unlike checkboxes that permit multiple selections, only one radio button can be selected in a group of radio buttons.

Basic HTML code

The basic HTML code uses standard radio buttons:

<form action="index.html" method="post">
    <h2 id="question">Which colour was Henri IV’s white horse?</h2>
 
    <ul id="answers">
        <li class="choice">
            <label for="yellow">
                <input type="radio" name="henri-iv-horse-colour" id="yellow" />
                Yellow
            </label>
        </li>
        <li class="choice">
            <label for="Green">
                <input type="radio" name="henri-iv-horse-colour" id="green" />
                Green
            </label>
        </li>
        <li class="choice">
            <label for="white">
                <input type="radio" name="henri-iv-horse-colour " id="white" />
                White
            </label>
        </li>
    </ul>
 
    <input type="submit" value="Submit information" />
</form>

Without JavaScript, a standard form is shown to the user.

Behaviour with JavaScript

With JavaScript, the behaviour of the simulated radio buttons must be the same as for standard radio buttons.

The scripts must handle the following points:

The resulting code in JavaScript is as follows:

<form method="post" action="index.html">
<form method="post" action="index.html">
    <h2 id="question">Which colour was Henri IV’s horse?</h2>
 
    <ul id="answers" role="radiogroup" aria-labelledby="question">
        <li class="choice" role="radio" aria-checked="false" tabindex="0">
            <img aria-hidden="true" src="images/unselected-radio-button.png" alt=" Not selected: ">
            Yellow
        </li>
        <li class="choice" role="radio" aria-checked="false" tabindex="-1">
            <img aria-hidden="true" src="images/unselected-radio-button.png" alt="Not selected: ">
            Green            
        </li>
        <li class="choice" role="radio" aria-checked="false" tabindex="0">
            <img aria-hidden="true" src="images/unselected-radio-button.png" alt=" Not selected: ">
            White            
        </li>
    </ul>
 
    <input type="submit" value="Submit information">
</form>

Démonstration