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

285
Views
Javascript: querySelector only selects 1 div. How do I get it to select all divs with the same class?

I'm using document.querySelector and I need it to select all divs that contains the class "test". So far querySelector selects the first div only.

How do I do that with my current JavaScript?

var removeCurveStroke = document.querySelector('.test');

if(removeCurveStroke.classList.contains('card-decorator-stroke')){
    removeCurveStroke.classList.remove("card-decorator-stroke");
    removeCurveStroke.classList.add("brand-curve-stroke");
}

Any help is gladly appreciated. Thanks

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

0

Use document.querySelectorAll('.test'); Documetnation for querySelectorAll

document.querySelectorAll('.card-decorator-stroke').forEach(function(removeCurveStroke) {
   removeCurveStroke.classList.remove("card-decorator-stroke");
   removeCurveStroke.classList.add("brand-curve-stroke");
});
.test{
 height: 1rem;
 background: blue;
}
.card-decorator-stroke {
  background: red;
}
.brand-curve-stroke {
  background: green;
}
 
<div class="test"></div>
<div class="test card-decorator-stroke"></div>

about 4 years ago · Juan Pablo Isaza Report

0

not recommendable but try document.getElementsByClassName("test")

about 4 years ago · Juan Pablo Isaza Report

0

You can try with jquery

$(".test .card-decorator-stroke").addClass("brand-curve-stroke").removeClass("card-decorator-stroke");
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!