Checkbox Example (Mixed State)
This example implements the Checkbox Design Pattern using div
element for a mixed state checkbox used in conjunction with a set of standard HTML checkboxes. This example also uses fieldset/Legend
elements to provide a grouping label for the checkboxes since the checkboxes are related to each other.
Similar examples include:
- Checkbox (Two State): Simple two state checkbox.
Example
Keyboard Support
Key | Function |
---|---|
Tab |
|
Space, or Enter |
|
Role, Property, State, and Tabindex Attributes
Role | Attribute | Element | Usage |
---|---|---|---|
checkbox |
div |
|
|
tabindex="0" |
div |
|
|
aria-controls="[IDREFS]" |
div |
|
|
aria-checked="false" |
div |
|
|
aria-checked="true" |
div |
|
|
aria-checked="mixed" |
div |
|
Javascript and CSS Source Code
- CSS: checkboxMixed.css
- Javascript: checkboxMixed.js
- Javascript: controlledCheckbox.js
HTML Source Code
<div id="ex1">
<fieldset>
<legend>
Sandwich Condiments
</legend>
<div role="checkbox"
class="group_checkbox"
aria-checked="mixed"
aria-controls="cond1 cond2 cond3 cond4"
tabindex="0">
All condiments
</div>
<ul class="checkboxes">
<li>
<label>
<input type="checkbox" id="cond1">
Lettuce
</label>
</li>
<li>
<label>
<input type="checkbox"
id="cond2"
checked="">
Tomato
</label>
</li>
<li>
<label>
<input type="checkbox" id="cond3">
Mustard
</label>
</li>
<li>
<label>
<input type="checkbox" id="cond4">
Sprouts
</label>
</li>
</ul>
</fieldset>
<script type="text/javascript">
function checkboxFocus(event) { event.currentTarget.parentNode.classList.add('focus'); }; function checkboxBlur(event) { event.currentTarget.parentNode.classList.remove('focus'); }; window.onload=function() { var i; var mixed = document.querySelectorAll('[role="checkbox"]'); for (i = 0; i < mixed.length; i++) { var m = new CheckboxMixed(mixed[i]); m.init(); } var checkboxes = document.querySelectorAll('input[type="checkbox"]'); for (i = 0; i < checkboxes.length; i++) { var cb = checkboxes[i]; cb.addEventListener('focus', checkboxFocus); cb.addEventListener('blur', checkboxBlur); } }
</script>
</div>