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

209
Views
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 answers
Answer question

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