Languages
[Edit]
EN

JavaScript - how to check if input checkbox is checked?

9 points
Created by:
AnnLen
1777

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>

 

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.