Languages
[Edit]
EN

JavaScript - how to check if input checkbox is checked?

9 points
Created by:
AnnLen
9180

In this article, we're going to have a look at how to check if checkbox is checked in Pure JavaScript.

Quick solution:

var checkbox = document.getElementById('my-checkbox');
var checked = checkbox.checked;

More detailed description of solution is placed below.

1. Vanilla JavaScript example

In this section checked property is used to get current state of checkbox.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <label>
    <input id="my-checkbox" type="checkbox" /> Agreement
  </label>
  <script>

    var checkbox = document.querySelector('#my-checkbox');

    checkbox.addEventListener('click', function() {
        console.log(checkbox.checked); // or this.checked
    });

  </script>
</body>
</html>

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join