Support

Account

Home Forums General Issues Prepare Field JS

Solving

Prepare Field JS

  • Hi,
    i’m using prepare_field hook in my functions.php in this way:

    function preselect_an_option( $field ) {
       
        if (isset($_GET['value'])){ 
            $field['value'] = $_GET['value']; 
        }
        return $field;
             
    }
    add_filter('acf/prepare_field/name=my_filed', 'preselect_an_option');

    But now I would get the value to set field from a JS function (instead of $_GET[‘value’]).
    So i need a kind of prepare_field hook JS.
    Anyone can help me?

    Thanks

  • There isn’t a prepare field on the JS side of things. To be honest, I’m not really sure what you’re trying to accomplish using JS. Do you want to set the field to something based on another field?

    The ACF documentation for custom JS is here https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/, other than this you need to look for similar questions on this forum or look for examples. I have a few examples of some custom JS for dynamic fields here that might also give you some ideas https://github.com/Hube2/acf-dynamic-ajax-select-example

  • Thanks for reply John.
    I try to explain with an example. Suppose i’ve 2 buttons outside acf form. If i click first one i want that my multiple select (post object type) default value is ‘one’. If i click second one selected value is ‘two’.

    Because page is already loaded i cant use prepare_field hook.

    I hope you could help me.
    Thanks

  • As I said above, that will require building your own custom JavaScript. Take a look at the examples I gave, there are several examples of different ways to set one field based on the selection of another field. Past that the JS you create will need to be specific to your fields.

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

You must be logged in to reply to this topic.