Support

Account

Home Forums General Issues When to use the ACF JavaScript API library?

Solved

When to use the ACF JavaScript API library?

  • Hi,

    I am having a bit of a hard time wrapping my head around as to when to use the ACF Javascript library. Can someone please share some examples? The documentation page goes straight into details does not offer much guidance on sample scenarios.

    Some of the things I usually do is manipulate ACF fields using jQuery by loading a javascript file that adds events to fields so I can update other field values when such events happen. For instance, this is what I am doing display a mixed dog breed based on two select fields that each hold a list of breeds:

    var primaryBreed = $('#primary-breed option:selected').text();
       var secondaryBreed = $('#secondary-breed option:selected').text();
       if ( -1 === primaryBreed.localeCompare(secondaryBreed)  || '' === secondaryBreed || primaryBreed === secondaryBreed ) {
          $('#acf-field_mdr_dog_text_breed_name').val( primaryBreed + ' X ' + secondaryBreed );
       }
       else {
          $('#acf-field_mdr_dog_text_breed_name').val( secondaryBreed + ' X ' + primaryBreed );
       }

    Should I be using the Javascript API fro this or would that be overkill?

    Thanks!

  • In your example you could use the api when setting the value of the third field. But I wold likely to id the same way because it is a text field.

    You could also use the api to get the value of the select fields, but again, is not necessary.

    On more complex fields, for example and image field or a color picker fields and others, in this case you would need to use the api because there are a lot of things that need to happen when the value is changed and it is a lot more complicated to get the field value in some cases and the api will make it easier in these cases.

    I generally stick with doing it myself without the api unless I run into things where I have a problem doing what I need to do.

  • @hube2 You’re the man, my friend, appreciate your feedback.

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

You must be logged in to reply to this topic.