function that make it impossible to uncheck a checked checkbox in javascript

Here's a function that prevents unchecking a checkbox once it has been checked:

index.tsx
function disableCheckbox(checkbox) {
  checkbox.addEventListener('click', function(event) {
    if (checkbox.checked) {
      checkbox.disabled = true;
    }
  });
}
166 chars
8 lines

You can pass the checkbox element as an argument to this function and it will add a click event listener to it. Once the checkbox is clicked and checked, it will be disabled and cannot be unchecked again.

Here's an example usage:

<input type="checkbox" id="my-checkbox">
<script>
  var myCheckbox = document.getElementById('my-checkbox');
  disableCheckbox(myCheckbox);
</script>
150 chars
6 lines

Note: The disableCheckbox() only disables the checkbox when it is clicked and checked. If you want to prevent unchecking programmatically, you would need to add additional checks and possibly store the state of the checkbox somewhere.

gistlibby LogSnag