Support

Account

Home Forums General Issues Limit Number of Checkboxes to select

Helping

Limit Number of Checkboxes to select

  • Hey Elliot and team!

    I am interested in limiting the checkboxes on one of our fields. I did some research and found some js that should handle this, but I cannot seem to get it to work as expected. I was hoping a second set of eyes could see where I was off.

    Here is the code I am using:

    
    add_action('admin_print_scripts','ssm_inline_scripts');
    function ssm_inline_scripts() { ?>
    <script>
    var limit = 4;
    jQuery('.field_key-field_5408de6b05aa9 input[type='checkbox']').on('change', function(evt) {
       if(jQuery(this).siblings(':checked').length >= limit) {
           this.checked = false;
       }
    });
    </script>
    <?php }
    

    Any thoughts? Thanks! -Rich

  • This is how I added support to limit the number of selectable checkboxes:

    
    acf.add_action('ready', function( $el ){
        var limit    = 4,
            selector = '.acf-field-573408b96dcd4 :checkbox',
            $boxes   = $(selector),
            limiter  = function () {
                if ($boxes.filter(':checked').length >= limit) {
                    // this.checked = false;
                    $boxes.not(':checked').attr('disabled', true).prop('disabled', true);
                } else {
                    $boxes.filter(':disabled').removeAttr('disabled').prop('disabled', false);
                }
            };
    
        if ($boxes.length) {
            limiter();
        }
    
        $(document).on('change.aqcpe.acf.careers', selector, limiter);
    });
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Limit Number of Checkboxes to select’ is closed to new replies.