Tell us what you are trying to do?
Limit the number of checkboxes allowed for a specific CPT. I have two CPT's - CPT-1 and CPT-2. Both of these CPT's use a checkboxes field called "states". I want to limit the number of checkboxes allowed for CPT-1. CPT-2 would not have a limit.
How can this be accomplished?
Is there any documentation that you are following?
The following support ticket contains a code snippet to limit the number of checkboxes. https://toolset.com/forums/topic/limiting-the-number-of-checkbox-selected/
Code:
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;
}
});
});
What is the link to your site?
staging - under development.
Hi,
Thank you for contacting us and I'd be happy to assist.
I've tested the code that you shared on my test website and it is working correctly.
Have you tried adding this code in your from's "JS editor"?
( example screenshot: hidden link )
Please also make sure that the slug field slug "tipo-de-massagem" matches the slug of your target checkboxes type field.
In case, it doesn't work, you're welcome to share temporary admin login details, along with the link to the form page.
Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi Waqar,
Thanks for the prompt reply.
I did not see the JS Editor on the post form and was going to insert using a snippet tool outside of Toolset.
To clarify, if I enter the code into the JS editor for CPT-1, will it only work for CPT-1? The field slug would be changed to "Listing" to match the slug for Listing. However, the field has the same slug for CPT-1 and CPT-2.
What I am trying to do is have a common field in both CPT's to use for a filter for both CPT's in the same view. Will this work?
Regards,
Chris
Hi Waqar,
I entered the code into the JS Editor for the post form for CPT-1, and it only limits the check boxes for CPT-1 and not CPT-2. So this looks like it does what I want.
Will this same code work for a taxonomy? By changing the slug to match the slug of the taxonomy/
Thanks again.
Thanks for the update and glad that it is working.
And yes, the same code will work for taxonomy term checkboxes too. You'll just have to replace the custom field key ( e.g. 'wpcf-tipo-de-massagem' ) with the taxonomy's slug ( e.g. 'category' ).
Thanks, Waqar.
I see the error in my code. I was entering the category custom field key as "wpcf-industry".
$('input:checkbox[name="wpcf-industry[]"]').......
I changed the code custom key field to "industry" and it works.
$('input:checkbox[name="industry[]"]').......
Thank you for the support.
Thank you, Waqar!
With the changes implemented, the function is now working properly.