Skip to content

Select element not associated with its intended label element #1927

@trichoplax

Description

@trichoplax

Describe the bug
A label for a select element has a for attribute that matches the name rather than the (non-existent) id of the select element. See app/views/search/_filters from line 10:

      <%= label_tag :predefined_filter, 'Predefined Filters', class: "form-element" %>
      <%= select_tag :predefined_filter, options_for_select([@active_filter[:name]],
                                                            selected: @active_filter[:name]),
          include_blank: true, class: "form-element js-filter-select", id: nil,
          data: { placeholder: "" } %>

This results in the following HTML in the browser:

  <label class="form-element" for="predefined_filter">Predefined Filters</label>
  <select name="predefined_filter" class="form-element js-filter-select select2-hidden-accessible" data-placeholder="" data-select2-id="3" tabindex="-1" aria-hidden="true">
    <option value="" label=" " data-select2-id="5">
    </option><option value="" data-select2-id="6">
    </option><option value="Positive" data-select2-id="7">Positive</option>
    <option value="Unanswered" data-select2-id="8">Unanswered</option>
    <option value="None" data-select2-id="9">None</option>
    <option value="Open" data-select2-id="10">Open</option>
  </select>

Note that the label has a for attribute of predefined_filter but the select does not have that id (in fact it has no id). The select has a name attribute of predefined_filter but this does not associate the label with the select - a for attribute only works with an id.

Expected behavior
There are 2 ways the label could be associated with the select:

  • The select could have an id of predefined_filter to match the for of the label.
  • The select could be contained within the label element (between the opening and closing tags along with the label text).

I do not know whether the use of the third party library Select2 limits what we can change here. If not, we should adopt one of these approaches.

Tracking where this occurs

Following Oaphi's comment please feel free to edit in any other places where this problem occurs:

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: accessibilityBugs/changes that affect accessibility. Other areas may also apply.area: frontendChanges to front-end code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions