I'm trying to implement more accessibility to the website I'm working on. I have a custom checkbox inside a button, followed by a list of checkboxes. When I'm using a screenreader, I expect the screen reader to tell me whenever a checkbox is unmarked/marked. This works for all the checkboxes, except the one inside my button. The purpose of the button is to mark/unmark all other checkboxes in the list (this is done by JavaScript). Everything works, except the screenreader is not saying anything when I unmark/mark it.
Here the code:
<button id="checkAll" class="btn btn-default checkAll checkAllInit"
data-target="#everyCheckbox">
<span class="icon icm icm-checkbox_select" role="checkbox"></span>
<span class="text">Unmark all</span></button>
What follows is basically a list of checkboxes, which all can be reached and understood by keyboard navigation and the screen reader. It's just the button that won't give the correct feedback.
Any suggestions appreciated.
It's not valid html. You can use https://validator.w3.org/nu/ to check your code. A <button>
cannot have any interactive components nested in it. See https://www.w3.org/TR/html53/sec-forms.html#the-button-element
Content model: Phrasing content, but there must be no interactive content descendant.
This will confuse the screen reader. You either need a simple button or a simple checkbox.
If you're not using a native <input type='checkbox'>
, then you need role='checkbox'
and aria-checked
. Just toggle the value of aria-checked
between true
and false
in your javascript and the screen reader will announce the change.
First of all, a button
element cannot contain any "interactive content", i.e. no other form controls, audio
elements, video
elements, etc.
Ideally, you should use native HTML elements, i.e. a form
element to wrap around the checkboxes and buttons, and the input
element with its type set to checkbox
. You'll also need to associate labels with the checkboxes. With these native elements, you don't need WAI-ARIA attributes such as role='checkbox'
and aria-checked
; these WAI-ARIA attibutes are actually redundant (see e.g. the article On HTML belts and ARIA braces .)
If you have good reasons to create custom checkboxes using a combination span
, CSS and JavaScript, you will need additional markup to make these checkboxes accessible:
tabindex="O"
to make sure that keyboard users can reach the checkbox,role='checkbox'
to enable screen readers to figure out what type of element they are dealing with,aria-checked
(with the values true or false) to enable screen readers to figure out the state of the checkbox,aria-labelledby
to associate a "label" with the checkbox.As you can see, if you use custom elements where HTML has native elements, you create a lot of extra work for yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With