Home ยป JavaScript ยป Check if Checkbox is Checked in JavaScript

Check if Checkbox is Checked in JavaScript

To check if a checkbox is checked in JavaScript, you can use the HTMLInputElement.prototype.checked property. Here's an example:

Check if Checkbox is Checked in JavaScript Example

const checkbox = document.querySelector('input[type="checkbox"]');

if (checkbox.checked) {
  console.log('The checkbox is checked');
} else {
  console.log('The checkbox is not checked');
}

In this example, we are using the querySelector() method to select the checkbox element on the page, and then we are checking its checked property to determine its state. If the property is true, then the checkbox is checked. Otherwise, it is not checked.

Here's an example of the output you might see from this code:

The checkbox is checked

This indicates that the checkbox is in a checked state.

Another way to check if a checkbox is checked is to use the HTMLInputElement.prototype.addEventListener() method, like this:

const checkbox = document.querySelector('input[type="checkbox"]');

checkbox.addEventListener('change', () => {
  if (checkbox.checked) {
    console.log('The checkbox is checked');
  } else {
    console.log('The checkbox is not checked');
  }
});

In this example, we are using the addEventListener() method to listen for the change event on the checkbox. When the event is triggered, we are checking the checked property to determine the checkbox's state.

The output of this code would be the same as the previous example:

The checkbox is checked

which indicates that the checkbox is in a checked state.

Check if a Checkbox is Checked by Class in JavaScript

To check if a checkbox is checked by its class name in JavaScript, you can use the document.querySelectorAll() method to select the checkbox elements and then use the Array.prototype.filter() method to find the checked ones. Here's an example:

const checkboxes = document.querySelectorAll('.checkbox-class');
const checkedCheckboxes = Array.from(checkboxes).filter(checkbox => checkbox.checked);

console.log(`Number of checked checkboxes: ${checkedCheckboxes.length}`);

In this example, we are using the querySelectorAll() method to select all elements with the checkbox-class class name, and then we are using the filter() method to find the ones that are checked. We are then logging the length of the checkedCheckboxes array to show how many checkboxes are checked.

Here's an example of the output you might see from this code:

Number of checked checkboxes: 2

This indicates that there are two checkboxes on the page with the checkbox-class class name, and both of them are checked.

You can also use the Array.prototype.forEach() method to iterate over the checkboxes and perform a specific action for each checked checkbox, like this:

const checkboxes = document.querySelectorAll('.checkbox-class');

checkboxes.forEach(checkbox => {
  if (checkbox.checked) {
    console.log('Checkbox is checked');
  } else {
    console.log('Checkbox is not checked');
  }
});

In this example, we are using the forEach() method to iterate over the checkboxes and check their checked property to determine their state. For each checked checkbox, we are logging a message to the console.

The output of this code would show a message for each checkbox on the page with the checkbox-class class name, indicating whether it is checked or not. For example, if there are two checked checkboxes and one unchecked checkbox on the page, the output might look like this:

Checkbox is checked
Checkbox is checked
Checkbox is not checked

which indicates that two of the checkboxes are checked and one is not checked.

Check if a Checkbox is Checked by ID in JavaScript

To check if a checkbox is checked by its id in JavaScript, you can use the document.getElementById() method to select the checkbox element and then check its checked property to determine its state. Here's an example:

const checkbox = document.getElementById('checkbox-id');

if (checkbox.checked) {
  console.log('The checkbox is checked');
} else {
  console.log('The checkbox is not checked');
}

In this example, we are using the getElementById() method to select the checkbox element with the checkbox-id id, and then we are checking its checked property to determine its state. If the property is true, then the checkbox is checked. Otherwise, it is not checked.

Here's an example of the output you might see from this code:

The checkbox is checked

This indicates that the checkbox is in a checked state.

Another way to check if a checkbox is checked by its id is to use the HTMLInputElement.prototype.addEventListener() method, like this:

const checkbox = document.getElementById('checkbox-id');

checkbox.addEventListener('change', () => {
  if (checkbox.checked) {
    console.log('The checkbox is checked');
  } else {
    console.log('The checkbox is not checked');
  }
});

In this example, we are using the addEventListener() method to listen for the change event on the checkbox. When the event is triggered, we are checking the checked property to determine the checkbox's state.

The output of this code would be the same as the previous example:

The checkbox is checked

which indicates that the checkbox is in a checked state.

Related:

  1. Check if an Array is Empty in JavaScript
  2. JavaScript - Remove Element on Click