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 successfully.
How do I do that with my current Javascript?
var removeCurveStroke = document.querySelector('.test'); if(document.querySelector('.test').classList.contains('card-decorator-stroke')){ removeCurveStroke.classList.remove("card-decorator-stroke"); removeCurveStroke.classList.add("brand-curve-stroke"); console.log("Test"); }
Any help is gladly appreciated. Thanks
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>
not recommendable but try document.getElementsByClassName("test")
You can try with jquery
$(".test .card-decorator-stroke").addClass("brand-curve-stroke").removeClass("card-decorator-stroke");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.