Support

Account

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');
    });