How to prevent checkbox from being checked in JavaScript

In this article, you are going to learn how you can prevent a checkbox from being checked using JavaScript.

Prevent checkbox from being checked
<input type="checkbox" id="my-checkbox">
<script>
  document.querySelector('#my-checkbox').onclick = (e)=>{
    e.preventDefault();
  }
</script>
Try it Yourself »

Use the preventDefault() method to stop the default behavior of a selected element. Learn more about preventDefault() method.