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
Toggle Switch correctly removes css class on false but doesn't put the class back on when true

Using a Bootstrap5 toggle switch (which is basically just a checkbox) to toggle on and off lines in a grid. I have it set up so if the toggle is true then add the class which adds lines for the grid and if it's false then remove class and the lines. When I toggle off the class that holds the style for the line gets removed correctly but when I toggle on then the class doesn't come back and toggle the lines on. I am also using a forEach() to loop through all the divs.

I have console.log within and they fire for each toggle so I am not sure what I am missing. Here is the code

JS

let gridToggleSwitch = document.querySelector("#gridToggle")

gridToggleSwitch.addEventListener('change', () => {

    let boxList = document.querySelectorAll('.box')

    let toggleChoice = gridToggleSwitch.checked

    if(toggleChoice == true) {
        console.log("LINES")

        boxList.forEach((e) => {
            console.log(e)

            e.classList.add('box')
        })

    } else if (toggleChoice == false) {
        console.log('NO LINES')

        boxList.forEach((e) => {
            console.log(e)

            e.classList.remove('box')
        })

    } else {
        console.log('woops')
    }

})

CSS:

.box {
    border: 1px solid black;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

SOLVED:

since the grid can change dynamically I needed to also dynamically add an ID to the divs for the grid and instead of using the forEach() for the classes I looped through the ID and added/removed the border style

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!