Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda