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

288
Visualizações
How can I check with JQuery if a checkbox is checked and if so add a class to the parent label?

I would like, on load of the page, to check if a checkbox is :checked Then if it is, add a class to the label. I currently have the following script but it doesn't work.

$(document).ready(function() {
    $('input').is(':checked') {
        $(this).parent('label').toggleClass('label--active');
    });
});

My HTML is:

<label class="container-checkbox" for="checkOne">
    <span class="checkbox-label">Option one</span>
    <input type="checkbox" id="checkOne">
    <span class="checkmark"></span>
</label>

I can not change the DOM order as these are custom checkboxes.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I guess you can use this:

$(document).ready(function() {
    let checkboxone = document.getElementById('checkOne');
  checkboxone.addEventListener('change',function() {
    checkboxone.parentNode.classList.toggle('label--active');
  });
});

How does is work?

Everytime when the checkbox is clicked, the class 'label--active' will be added / removed.

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