Skip to content

Instantly share code, notes, and snippets.

@tahmid-ul
Created September 29, 2023 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tahmid-ul/4b98355947f9b63b3be1c57d4cd8759c to your computer and use it in GitHub Desktop.
Save tahmid-ul/4b98355947f9b63b3be1c57d4cd8759c to your computer and use it in GitHub Desktop.
// Add the JS code to your Forms custom JS section.
// Add a container class with the name - max_1_items on your checkbox field.
function maxItemsCheck(containerClass, maxChecked) {
const checkboxes = document.querySelectorAll('.' + containerClass + ' input');
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener('change', function() {
const checkedCheckboxes = document.querySelectorAll('.' + containerClass + ' input:checked');
const count = checkedCheckboxes.length;
if (count > maxChecked) {
alert('You can not check more than ' + maxChecked + ' items');
this.checked = false;
}
});
});
}
// Example Use case
maxItemsCheck('max_1_items', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment