Support

Account

Home Forums ACF PRO Copy relationship field values to another relationship field Reply To: Copy relationship field values to another relationship field

  • Nevermind, I solved the problem in a much easier way.
    In case someone else needs a similar solution, here is the code for solution:

    jQuery(document).ready(function($){
      function ivab_copy_field_values(source_field_id, target_field_id) {
        $(source_field_id).find('.values li input').each(function() {
          fieldValue = $(this).val();
          targetLi = $(target_field_id).find('.choices li span[data-id="'+fieldValue+'"]');
          if ($(targetLi).length) {
            $(targetLi).trigger('click');
          }
        });
      }
    
      if ($('#normal-sortables').length) {
        $('.acf-field-56c3703fffcbd .acf-label')
          .append('<button class="button button-large copybutton" type="button" data-targetfield="#acf-field_56c3703fffcbd" data-sourcefield="#acf-field_56c372ebcec74">Copy from B side</button>');
        $('.copybutton').on('click', function() {
          console.log($(this));
          sourceField = $(this).attr('data-sourcefield');
          targetField = $(this).attr('data-targetfield');
          ivab_copy_field_values(sourceField, targetField);
        });
      }
    });