Support

Account

Home Forums Backend Issues (wp-admin) Add Term ID as class to checkbox

Solving

Add Term ID as class to checkbox

  • I’ve been trying to add the term ID as a class to a checkbox but so far I have had no luck. Would you point me in the right direction? I currently have a custom taxonomy called Region and another taxonomy called Company. What I want to do is to only show the company under the company taxonomy(checkbox) associated with the regions term as selected in the Region taxonomy(select). Any help would be greatly appreciated. Thank you so much!

    Here is what I have so far:

    function filter_region_taxonomy() { ?>
    <script type="text/javascript">
    (function($) {
    // Document Ready
    $(document).ready(function() {
    $('#acf-standard_region select').trigger('change');
    });
    $('#acf-standard_region .acf-taxonomy-field select').live('change', function() {
    var value = $(this).val();
    $( "#acf-standard_company:checkbox" ).addClass("compclass");
    <?php
    $taxonomy = "company";
    $term_id = 460; <!-- how do I get the term ID based on the checkbox value? -->
    ?>
    var regionVal = "<?php the_field( 'jobsite_region', 'company_' . $term_id ); ?>";
    if( value == "7" ) {
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "southern no-class" ).addClass(regionVal);
    $('#acf-standard_company').show();
    $('ul.acf-checkbox-list li').show();
    }
    else if( value == "9" ) {
    
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "northern no-class" ).addClass(regionVal);
    $('#acf-standard_company').show();
    $('ul.acf-checkbox-list li').show();
    }
    else {
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "southern northern" ).addClass( "no-class" );
    $('#acf-standard_company').hide();
    $('ul.acf-checkbox-list li').hide();
    }
    
    });
    
    })(jQuery);
    </script>
    
    <?php }
    
    add_action('acf/input/admin_head', 'filter_region_taxonomy');
  • Hi @khleomix

    Can you please put your code on either codepen or pastebin or at least as a code element here 🙂 It’s very hard to read as plain text.

  • @jonathan Thanks, I’ve added it as a code element. Any help would be greatly appreciated.

  • Been playing around and so far this is my code that works, it gets the value of the checkbox but only on the selected/saved one. The code below gives me “southern 360” where 360 is the value of the saved/selected checkbox and southern is the correct region if based on the 360 ID. This is the same on all checkbox divs though and what I need is for each checkbox div to show the ID value of its respective checkbox.

    How can I change it so that it will add the value of the checkbox per checkbox and not the selected one?

    function filter_region_taxonomy() { ?>
    <script type="text/javascript">
    (function($) {
    // Document Ready
    $(document).ready(function() {
    $('#acf-standard_region select').trigger('change');
    });
    $('#acf-standard_region .acf-taxonomy-field select').live('change', function() {
    var value = $(this).val();
    $( "#acf-standard_company:checkbox" ).addClass("compclass");
    
    if( value == "7" ) {
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "southern no-class" ).addClass(("<?php $terms = get_the_terms( get_the_ID(), 'company'); if( !empty($terms) ) { $term = array_pop($terms); $term_id = $term->term_id; $regionVal = get_field('jobsite_region', 'company_' . $term_id ); echo $regionVal . ' ' . $term_id; } ?>"); 
    $('#acf-standard_company').show();
    $('ul.acf-checkbox-list li').show();
    }
    else if( value == "9" ) {
    
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "northern no-class" ).addClass(("<?php $terms = get_the_terms( get_the_ID(), 'company'); if( !empty($terms) ) { $term = array_pop($terms); $term_id = $term->term_id; $regionVal = get_field('jobsite_region', 'company_' . $term_id ); echo $regionVal . ' ' . $term_id; } ?>");
    $('#acf-standard_company').show();
    $('ul.acf-checkbox-list li').show();
    }
    else {
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "southern northern" ).addClass( "no-class" );
    $('#acf-standard_company').hide();
    $('ul.acf-checkbox-list li').hide();
    }
    
    });
    
    })(jQuery);
    </script>
    
    <?php }
    
    add_action('acf/input/admin_head', 'filter_region_taxonomy');
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.