HOWTO · JavaScript
JavaScript Checkbox onChange
Learn three ways to handle a JavaScript checkbox change and read its checked state.
On this page
A checkbox’s change event lets you respond when its checked state changes. Read the current Boolean value from the element’s checked property. You can connect a handler with an inline onchange attribute, assign the element’s onchange property, or register a change listener with addEventListener.
Handle Checkbox Changes with the change Event
For a checkbox, the change event fires when the user changes its checked state; it does not wait for the element to lose focus. The event tells your code that the state changed, while checked tells you the current state. Choose one of the following patterns for a given element so the response logic has a clear owner.
Use an Inline onchange Handler
An inline handler places the function call in the checkbox’s HTML. In this handler context, this refers to the checkbox element, so the function can read its checked property directly. A label associates descriptive text with the control, and the result paragraph provides a place to show the state.
<label>
<input type="checkbox" onchange="showState(this)">
Enable notifications
</label>
<p id="result" aria-live="polite"></p>
<script>
function showState(checkbox) {
document.getElementById("result").textContent = checkbox.checked;
}
</script>
The onchange HTML attribute names the handler to call. textContent displays the Boolean as plain text. Inline handlers can be convenient for a small example, but they mix behavior with markup and can be less suitable when a project keeps those concerns separate.
Assign an onchange Property Handler
To keep the handler in JavaScript, give the checkbox an id, select it, and assign a function to its onchange property. The function receives an event object if it declares a parameter; a parameter is unnecessary when the function reads the selected element directly.
<label>
<input type="checkbox" id="notifications">
Enable notifications
</label>
<p id="result" aria-live="polite"></p>
<script>
const checkbox = document.getElementById("notifications");
const result = document.getElementById("result");
checkbox.onchange = function () {
result.textContent = checkbox.checked;
};
</script>
Assigning the property sets the element’s handler. Assigning another function to the same property later replaces the previous one, so use a listener when multiple independent pieces of code need to respond.
Register a change Listener with addEventListener
addEventListener registers a callback for the named event. This pattern also receives the event object as its callback argument; event.currentTarget identifies the element on which this listener was registered.
<label>
<input type="checkbox" id="notifications">
Enable notifications
</label>
<p id="result" aria-live="polite"></p>
<script>
const checkbox = document.getElementById("notifications");
const result = document.getElementById("result");
checkbox.addEventListener("change", function (event) {
result.textContent = event.currentTarget.checked;
});
</script>
A listener can be added without replacing handlers registered elsewhere. Use the event name change, rather than onchange, as the first argument to addEventListener.
Read the Checkbox’s checked State
The checked property is Boolean: it is true when the checkbox is selected and false when it is not. Read it inside the handler to get the state after the change. In a browser, the examples above write that value to the result region; replace that assignment with the action your interface needs, such as enabling a related control.
This small JavaScript example isolates the state-reading logic. The browser examples above show how to connect that logic to a real checkbox event.
function showState(checkbox) {
return checkbox.checked;
}
const checkbox = { checked: true };
console.log(showState(checkbox));
true
The example prints the Boolean value returned from checked. The JavaScript example is self-contained; the HTML snippets are browser-page examples and are not shown as executed output.