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
jQuery alternative to selecting an element by chaining querySelector

Given the following html:

<div class="container">
    <form class="form" id="login">
        <h1 class="form__title">Login</h1>
        <div class="form__message form__message-error"></div>
    </form>
</div>

Is there an easy way to reference the element of class "form__message" ? I know this is the only element of this class in this example, but potentially there could be more. So I was trying the following to remove the form__message-error CSS-properties:

 $("#login .form__message").classList.add("form__message--error");

This, however, does not find the correct element, whereas the following works:

document.querySelector("#login").querySelector(".form__message").classList.remove("form__message--error");

I am looking for a jQuery alternative to the last statement.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're mixing jQuery and DOM methods. The jQuery equivalent of .classList.add() is .addClass().

$("#login .form__message").addClass("form__message--error");

You don't need .find() with querySelector(), you can use the same selector as with jQuery:

document.querySelector("#login .form__message").classList.add("form__message--error");
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!