Skip Navigation

[Resolved] Limit number of taxonomy terms

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.

Tagged: 

This topic contains 1 reply, has 1 voice.

Last updated by geophray 6 years, 1 month ago.

Author
Posts
#325696

I am trying to figure out if there is a way to limit the number of taxonomy terms on a CRED form that an end user can select from each unique list of taxonomy terms. Is this something that CRED can already do or would it require custom programming?

For example, lets say I have 25 terms in a taxonomy but only want to allow an end user to select up to 5 of them. Is there a way to pop up an error if they have selected more than 5, or to disable the remaining (unchecked) checkboxes in that taxonomy once five have been selected?

#325765

You can disregard this message. I came up with a custom jQuery script that does what I need it to.

For anybody that is interested, here is the code:

var countCheckedTopics = function() {
  var nTopics = jQuery( "div.cred-taxonomy-topic input:checked" ).length;
    if( nTopics >= 5 ){
        jQuery( "div.cred-taxonomy-topic input[type=checkbox]" ).not( ":checked" ).prop({
            disabled: true
        });           
    } else {
        jQuery( "div.cred-taxonomy-topic input[type=checkbox]" ).not( ":checked" ).prop({
            disabled: false
        });
    };
};
countCheckedTopics();
 
jQuery( "div.cred-taxonomy-topic input[type=checkbox]" ).on( "click", countCheckedTopics );


var countCheckedTechniques = function() {
  var nTechniques = jQuery( "div.cred-taxonomy-techniques input:checked" ).length;
    if( nTechniques >= 5 ){
        jQuery( "div.cred-taxonomy-techniques input[type=checkbox]" ).not( ":checked" ).prop({
            disabled: true
        });           
    } else {
        jQuery( "div.cred-taxonomy-techniques input[type=checkbox]" ).not( ":checked" ).prop({
            disabled: false
        });
    };
};
countCheckedTechniques();
 
jQuery( "div.cred-taxonomy-techniques input[type=checkbox]" ).on( "click", countCheckedTechniques );