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

210
Visualizações
Set focus to input element within find method

Given a form with error states, when the user hits a submit button, the focus should move to the first field with an error state. I'm able to grab the first instance of a field with an error state using:

[...document.querySelectorAll(".form-w1")].find(el => el.classList.contains("error"))

but can't get the input field nested in the div to set focus to it.

console.log([...document.querySelectorAll(".form-w1")].find(el => el.classList.contains("error")))
<span class="form-w1">
  <label>
    <span>First Name</span>
  </label>
  <span>
    <input id="f1" type="text" required="" placeholder=" ">
    </span>
</span>
<span class="form-w1 error">
  <label>
    <span>Last Name</span>
  </label>
  <span>
    <input id="f2" type="text" required="" placeholder=" ">
    </span>
</span>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In this case, there's no need to use .find().

Simply use a selector that fully selects what you're looking for:

document.querySelector('.form-w1.error input')?.focus();
about 4 years ago · Juan Pablo Isaza 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