How to prevent checkbox from being unchecked in JavaScript

In this article, you are going to learn how you can prevent a checkbox from being unchecked using JavaScript preventDefault() method without disableing it.

Prevent checkbox from being checked
<input checked="" 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 particular element. Learn more about preventDefault() method.