2

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.

gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • I just noticed that your HTML is invalid: Input elements have an empty content model. You shouldn't put their label inside the input tags. Browsers are tolerant and ignore your closing input tag any way, but to be sure I'd advise correcting it. ` Reason 1` – Andy May 28 '21 at 18:01
  • thanks @Andy, yeah you are right. Will fix it. any idea why input has an empty content model ? – gaurav5430 May 30 '21 at 04:23
  • 1
    Hm. I could imagine that it's because it got it's content in the value attribute, which puts more constraints on it than DOM content can achieve, like validations. And there wouldn't be other content to an input, so it's simply empty. – Andy May 31 '21 at 08:36

2 Answers2

4

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.

QuentinC
  • 12,311
  • 4
  • 24
  • 37
-1

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;
}
fundor
  • 1
  • 4
    Hey! Thanks for answering. I am not looking for ways to achieve this functionality in javascript or html, I am looking for accessibility patterns that can make it easier for people with screenreaders to identify that the text area is related to the 'other' checkbox – gaurav5430 May 05 '21 at 19:17