Skip Navigation

[Resolved] Limiting the number of checkbox selected

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 1 voice.

Last updated by FelipeP5703 5 years, 2 months ago.

Author
Posts
#1362825

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

#1362849

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.