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

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

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 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