Support

Account

Home Forums Backend Issues (wp-admin) I have JS in ACF fields working but how about only when a tab is opened?

Solved

I have JS in ACF fields working but how about only when a tab is opened?

  • I have basic JS running and appending to/around fields as I want to for some advanced tasks (ref: https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/).

    However what I’d love to do is run something a little intensive only when required which means when a specific tab is clicked on to become visible.

    Has anyone done this and have some neat code for reference they can share ?

  • So I fixed this myself with two new conditions in a custom JS file that uses jquery within the WP admin.

    
    // run the code when the specific tab of the form group is clicked on
    jQuery(document).on('click','*[data-key="field_123456789"]', function(event){
        runCustomCodeFunction();
    });
    // also run my code if the page loads and this tab is visible (it isn't by default as it isn't the first tab)
    if ( jQuery(".acf-field-123456789:visible").length > 0 ) {
        runCustomCodeFunction();
    }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.