Tell us what you are trying to do? I'm trying to limit the number of checkbox selected on the CRED form. I searched on the forum but could not find anything.
Thus I found this site: hidden link
I tried implementing it with this code
(function checkBoxLimit() {
var checkBoxGroup = document.forms['wpt-form']['wpcf-tipo-de-massagem'];
var limit = 10;
for (var i = 0; i < checkBoxGroup.length; i++) {
checkBoxGroup[i].onclick = function() {
var checkedcount = 0;
for (var i = 0; i < checkBoxGroup.length; i++) {
checkedcount += (checkBoxGroup[i].checked) ? 1 : 0;
}
if (checkedcount > limit) {
console.log("You can select maximum of " + limit + " checkboxes.");
alert("You can select maximum of " + limit + " checkboxes.");
this.checked = false;
}
}
}
})( jQuery );
The checkbox custom field slug is tipo-de-massagem. I might be doing something wrong with the javascript code. Or if there is a better solution please advise. I'm trying to limit to 10 selected checkboxes.
Thank you
I was able to find the solution in this thread: https://toolset.com/forums/topic/limit-number-of-checkboxes-allowed/
I included a message for my own users in the code below:
jQuery(document).ready(function ($) {
var tema_limit = 10;
$('input:checkbox[name="wpcf-tipo-de-massagem[]"]').on('change', function(evt) {
if($('[name="wpcf-tipo-de-massagem[]"]:checked').length >= tema_limit+1) {
console.log("You can select maximum of " + tema_limit + " checkboxes.");
alert("You can select maximum of " + tema_limit + " checkboxes.");
this.checked = false;
}
});
});
My slug is "tipo-de-massagem" please change yours to suit your needs.
My issue is resolved now.