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

121
Visualizações
JavaScript Checkbox Event Handler

How can i fire an event when a checkbox is checked or unchecked in JavaScript?

I have a checkbox. When I check it, I want the variable productionState to be set to true, and when I uncheck, I want it back to false. What I have doesn't work. Am I missing something? 

HTML

<div class="form-check ">

<input class="form-check-input" type="checkbox" value="" id="checkBox">

<label class="form-check-label" for="checkBox">

Production State

</label>

</div>

JavaScript

let productionState = false

const checkbox = document.getElementById("flexCheckDefault");

checkbox.addEventListener("change", (event) => (productionState = true));
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

let productionState = false

const checkbox = document.getElementById("flexCheckDefault");

checkbox.addEventListener("change", (event) => {
productionState = checkbox.checked;
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe this is what you are looking for?

let productionState=false;
document.getElementById("flexCheckDefault").addEventListener("change",ev => console.log(productionState=ev.target.checked));
<div class="form-check ">
<input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">Production State</label>
</div>

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