Sometimes, we want to get value of HTML checkbox from onclick or onchange events with JavaScript.
In this article, we’ll look at how to get value of HTML checkbox from onclick or onchange events with JavaScript.
How to get value of HTML checkbox from onclick or onchange events with JavaScript?
To get value of HTML checkbox from onclick or onchange events with JavaScript, we get the checked
property.
For instance, we write
<label><input type="checkbox" />Checkbox</label>
to add a checkbox.
Then we write
const checkbox = document.querySelector("input");
checkbox.onclick = (e) => {
console.log(e.target.checked);
};
to select the checkbox with querySelector
.
Then we set its onclick
property to a function that logs the checked value we get from e.target.checked
.
Conclusion
To get value of HTML checkbox from onclick or onchange events with JavaScript, we get the checked
property.