Support

Account

Home Forums ACF PRO How to execute a function on change event in select2?

Solving

How to execute a function on change event in select2?

  • Hi,

    I need be able to execute a function on change event in a select2 field.
    This is populated based in taxonomy, as taxonomies doens’t wokr as conditional logic field, i need be able to hide or display a text field based on select2 value.

    But, i’m not able to do it, because i can’t find the selector, or bind the change event to this.

    Can someone help me please?

    Note: I’m populating the form in front-end with ajax, everything works fine, but this point is driving me crazy.

    Thanks

  • I’m trying to do exactly the same – it’s a pity that the taxonomy field doesn’t provide conditional logic.

  • Hi @nscdde02 and @mmjaeger

    I think you can use this code:

    <script>
    (function($) {
    $(document).ready(function(){
    
    	$("#acf-field_1234567890-input").on("change", function() {
            console.log($(this).val());
        });
    });
    
    })(jQuery);
    </script>

    Where field_1234567890 is your taxonomy field key.

    I hope this helps.

  • @james – THANK YOU! I wrestled with this issue for hours, until I finally found your post.

    I used to have an ACF select2 event that worked prior to ACF 5.4+:

    $("#acf-field_1234567890").on("change", function() { }

    In your answer you mentioned that the selector is actually #acf-field_1234567890-input. That solved it for me. I guess at some point ACF changed the select2 wrapper or version? At any rate, thank you! Your code above solved it for me.

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

You must be logged in to reply to this topic.