In JavaScript, you can check if a checkbox is checked by accessing the checked
property of the checkbox element. Here's an example:
const checkbox = document.getElementById('myCheckbox');
if (checkbox.checked) {
// The checkbox is checked
}
In JavaScript, you can check if a checkbox is checked by using its checked
property. Here's an example:
<input type="checkbox" id="myCheckbox">
// Select the checkbox element
const checkbox = document.getElementById('myCheckbox');
// Check if it's checked
if (checkbox.checked) {
console.log('Checkbox is checked');
} else {
console.log('Checkbox is not checked');
}
checked
property of a checkbox element returns a boolean:true
if the checkbox is checked.false
if it's not.<input type="checkbox" id="myCheckbox">
<button onclick="checkCheckbox()">Check Status</button>
<script>
function checkCheckbox() {
const checkbox = document.getElementById('myCheckbox');
if (checkbox.checked) {
alert('The checkbox is checked!');
} else {
alert('The checkbox is not checked!');
}
}
</script>
This method works for any checkbox element, regardless of its name or ID. Here is a live example :