Support

Account

Home Forums Front-end Issues Add CSS class when a checkbox is checked

Solving

Add CSS class when a checkbox is checked

  • Hi!
    I have a group of 12 checkboxes made with ACF, where each checkbox is a month:
    jan : Jan / feb : Feb / mar : Mar / etc.

    What i want to achieve is to display those 12 values in the frontend, with a particular CSS class if the month has been checked in the backend. Ej:

    <span class="selected">Jan</span>
    <span>Feb</span>
    <span>Mar</span>
    <span class="selected">Apr</span>
  • Hi @fedexfedex

    From the Checkbox documentation you can make use of a conditional like so:

    <?php
    
    // vars	
    $choice = get_field('months');
    
    // check
    if( $choice && in_array('jan', $choice) ): 
    <span class="selected">Jan</span>
    elseif( $choice && in_array('feb', $choice) ):
    ...
    ?>
    <?php endif; ?>
  • thanks @acf-support.

    actually i need to ALWAYS show all 12 months in the frontend (inside a span tag), and the months that have been selected in the backend should have the class “selected”.

    following the example in the docs, i’m only getting the selected months displayed in the frontend.

  • Hi @fedexfedex

    Thanks for the clarification.

    In such a case, you will need to use the getf_field_object as opposed to get_field.

    Let me know how it goes.

  • sorry but i cant get it to work.
    thanks anyway.

  • Hi @fedexfedex,

    Thanks for the follow up.

    Kindly open a more personalised support ticket at [email protected]

    Hope to hear from you soon.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Add CSS class when a checkbox is checked’ is closed to new replies.