Home › Forums › General Issues › JS event listener for sub fields › Reply To: JS event listener for sub fields
I have played with the ACF JS API and I’ve never been able to get it to work with sub fields when I need to detect a field changing or update sibling fields.
Instead I have always had to add a change action by targeting the field using jquery and using doing it on my own
// example event on change of sub field
// this needs to be on on the document level because rows and be added
// field_XXXXXX == sub field key to create action for
$(document).on('change', '[data-key="field_XXXXXX"] .acf-input select', function(e) {
// get the target of the change
target = $(e.target);
// get the row of the repeater
row = target.closest('.acf-row');
// get another field in the row
other_field = row.find('[data-key="field_YYYYY"] input');
// update value
other_field.val('new value');
// trigger change so that ACF knows it has been changed
other_field.trigger('change');
});
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.