Computer >> Computer tutorials >  >> Programming >> Javascript

How to check whether a checkbox is checked in JavaScript?


To check whether a checkbox is checked, try to run the following code. It returns true if the checkbox is checked, else false −

Example

<html>
   <head>
      <script>
         function myFunction(){
            var result = document.getElementById("check").checked;
            alert(result);
         }
      </script>
   </head>
   <body>
      <label><input id="check" type="checkbox" >One</label><br>
      <button onclick="myFunction()">Check value</button>
   </body>
</html>