accessibility - grouped checkboxes where one checkbox has a related textarea

503 Views Asked by At

I have a feedback form where I am asking users for a reason for cancellation, something like this:

label {
  display: block
}

button {
  display: block;
}
<form>
  <fieldset>
    <legend> Reason for cancellation? </legend>
    <label>
      <input type="checkbox"> Reason 1 </input>
    </label>
    <label>
      <input type="checkbox"> Reason 2 </input>
    </label>
    <label>
      <input type="checkbox"> Reason 3 </input>
    </label>
    <label>
      <input type="checkbox"> Reason 4 </input>
    </label>
    <label>
      <input type="checkbox"> Other </input>
    </label>
    <textarea aria-label="other reason"></textarea>
    <button> Submit </button>
  </fieldset>
</form>

The textarea is related to the Other checkbox, i.e, it only gets activated if the user selects the Other option, otherwise it remains disabled

How do I represent that association in markup?

NOTE: I have seen some examples, specifically Google Forms, and Search Engine Journal. Google forms solves this issue by placing the textbox next to the checkbox, the textbox is always enabled, and as soon as you focus on the textbox, the checkbox gets automatically checked.

Search Engine Journal, does not explicitly associate the controls, but they do mention it in the checkbox label, to fill in the reason below.

2

There are 2 best solutions below

1
fundor On

You could just hide the textarea by default and let it show up with a small javascript to toggle a class to set display to block.

<body>
    <form>
        <fieldset>
        <legend> Reason for cancellation? </legend>
        <label>
            <input type="checkbox"> Reason 1 </input>
        </label>
        <label>
            <input type="checkbox"> Reason 2 </input>
        </label>
        <label>
            <input type="checkbox"> Reason 3 </input>
        </label>
        <label>
            <input type="checkbox"> Reason 4 </input>
        </label>
        <label>
            <input type="checkbox" class="otherCheckbox"> Other </input>
        </label>
        <textarea aria-label="other reason" class="otherTextarea"></textarea>
        <button> Submit </button>
        </fieldset>
    </form>
</body>
<script>
    let otherCheckbox = document.querySelector(".otherCheckbox")
    let otherTextarea = document.querySelector(".otherTextarea")

    otherCheckbox.addEventListener("change", e => {
        otherTextarea.classList.toggle("show")
    })
</script>

Added to css:

.otherTextarea {
  display: none;
}
.show {
  display: block;
}
0
QuentinC On

Answer on using ARIA

You can use aria-controls, it seems like a good fit for your case. it looks more appropriate than aria-owns, see this question about difference between aria-own and aria-controls.

However, screen reader support is quite inconsistent, and even if it is supported, it's at the end quite rarely known and used by screen reader users. Therefore, in addition, it's always good to add a precision in clear text like you suggest e.g. "Other reason (please explain below)". Adding that indication in the label of the checkbox is a good choice. This added precision will anyway never by harmful to anyone, so you have no reason not to do it.

Answer on the UX design

If you have added the precision "please explain below", there's really no problem in enabling the textarea depending on the checkbox. Simply, make sure that the textarea come after the enabling checkbox in tab order, so to ensure that the user won't miss it and won't need to go back and forth to fill it in. Unless you have a weird design, it should normally already be the case.

The other alternative, checking the checkbox automatically when entering a reason is equally no problem, but you need to be more carful on when you do it:

  • Don't check the checkbox on focusing the textarea, otherwise keyboard only users will trigger it when they don't want to, and even normal users may click on the textarea and then change their mind
  • Don't check the checkbox on entering a character in the textarea. It's probably a bad idea because I may start typing something, and finally clear everything and change my mind

What about checkign the checkbox when the textarea loses the focus while being non-empty ?

You may, optionally, show a snackbar or something like that with aria-live=polite, telling that the checkbox as been checked automatically because you ahve entered something. This kind of bonus indication on things modified automatically would be quite useful in more complex forms, but for your case as it is presented here, it's totally superfluous because the relationship is obvious.