Home › Forums › General Issues › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.