Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda