how to close a check box on link click with class in javascript

Assuming you have a checkbox with a class and a link that triggers the checkbox, you can use the following code to close the checkbox on link click:

HTML:

index.tsx
<input type="checkbox" class="my-checkbox">
<a href="#" class="close-checkbox">Close Checkbox</a>
98 chars
3 lines

JavaScript:

index.tsx
const checkbox = document.querySelector('.my-checkbox');
const closeLink = document.querySelector('.close-checkbox');

closeLink.addEventListener('click', function(event) {
  event.preventDefault();
  checkbox.checked = false;
});
231 chars
8 lines

This code adds an event listener to the link with class 'close-checkbox', which prevents the default behavior of the link (navigating to a different page) and sets the 'checked' property of the checkbox to 'false', effectively closing it.

gistlibby LogSnag