Created
September 29, 2023 04:33
-
-
Save tahmid-ul/4b98355947f9b63b3be1c57d4cd8759c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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