Skip Navigation

[Resolved] Limiting Number of Checkboxes Checked for Specific CPT

This support ticket is created 2 years, 3 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#2263243

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.

#2263311

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#2263659

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

#2263675

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.

#2264325

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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' ).

#2264819

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.

#2264825

Thank you, Waqar!

With the changes implemented, the function is now working properly.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.