Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda