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

119
Views
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 answers
Answer question

0

let productionState = false

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

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

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 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!