Conditional Logic Switch/ Field group without updating the page

Support MB Conditional Logic Conditional Logic Switch/ Field group without updating the page

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #48450
    Oli ThompsonOli Thompson
    Participant

    Hi there,

    I just wondered how I could show/hide content based on an on/off switch field..
    (without having to reload the page)

    I have tried adding this code to the WP Admin Footer area in WPCodebox...
    (I don't really know what i'm missing)

    jQuery(function($) {
      setTimeout(function () {
        const $switch = $('input[name="is_framed"]');
        const $framingOptions = $('.Framing-options');
    
        function toggleFramingOptions() {
          const isChecked = $switch.prop('checked');
          console.log('Switch is checked:', isChecked); // debug
          if (isChecked) {
            $framingOptions.show();
          } else {
            $framingOptions.hide();
            $framingOptions.find('input[type="checkbox"]').prop('checked', false);
          }
        }
    
        toggleFramingOptions(); // run once on load
        $switch.on('change', toggleFramingOptions); // run when changed
      }, 300); // slight delay (adjust if needed)
    });
    

    Would appreciate any help anyone can give..
    Oli

    #48457
    PeterPeter
    Moderator

    Hello Oli,

    Is the element Framing-options created by Meta Box plugin? If so, you can simply follow the documentation to show/hide the field based on the switch field
    https://docs.metabox.io/extensions/meta-box-conditional-logic/#with-ui

    If not, it is a custom element on the editing page, you can follow this one to use conditional logic outside of the field group
    https://docs.metabox.io/extensions/meta-box-conditional-logic/#using-outside-field-groups

    #48458
    Oli ThompsonOli Thompson
    Participant

    Yes the Framing-options is a field group.

    The issue is that although condition logic rules work when I press "update", the desired outcome is that I would like the field group to be automatically shown/ hidden when the switch is activated (without pressing the "update" button).

    For this particular usecase I am not sure where to look. I tried copying these pages into chatgbt and asking what to do but I can't figure out how to implement the jquery method "if applicable"...

    #48459
    Oli ThompsonOli Thompson
    Participant

    here is my structure..

    Commission Type (cloneable group):
    --> Type:
    --> Switch (for framing options):

    Framing Options (cloneable group to be shown/ hidden by switch):
    --> Frame:
    --> Price:

    #48464
    PeterPeter
    Moderator

    Hello,

    >> I would like the field group to be automatically shown/ hidden when the switch is activated

    Yes, it should work like that because the Conditional Logic feature will show/hide the elements using JavaScript. Please share your site admin account by submitting this contact form https://metabox.io/contact/
    I will take a look.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.