Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
accessibility: adding label to custom input element in html

We have some custom components which can be used for user input (CustomSelect, SegmentedControl, CustomCheckbox, etc).

In some cases, these internally use the semantic html tag like radiogroup or select, but in some cases, the markup is completely written using div and span elements and accessibility is provided using aria attributes. For such implementations, is there a way to associate a label element to them, to make them behave like native form inputs wrt label behaviour, or better, is there a way to get them treated like native form inputs?

Things that come to mind:

  • use a visually hidden html form element which somehow wraps the custom, and somehow hide its semantics.
  • fieldset and legend might make sense in some cases
  • using javascript to imitate label functionality

Are there any existing patterns for this?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to do three things:

  1. Visually code it so that the label looks like it's associated with your custom element, typically because the label is to the left and the element is to the right (in LTR languages).
  2. Programmatically associate the label with the element by using aria-labelledby.
  3. Add javascript so that mouse clicking on the label moves the focus to the element.

Note that you can use a real <label> element but it would mainly be for more readable code, so it's easy to spot the label associated with the element. You most likely (*) cannot use the for attribute of the <label> element because the ID specified must be of a "labelable element", which is defined as:

  • button
  • input
  • meter
  • output
  • progress
  • select
  • textarea

(*) You can use the for attribute if you have a custom element with a form-associated attribute of true. A <label> can be used on a "form-associated custom element"

See https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-face-example.

I have not created a custom form element before but the doc sounds like a real <label for="ID"> can be used with it, in which case you don't need aria-labelledby or javascript.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!