Support

Account

Home Forums Feature Requests Repeater Field Copier

Solving

Repeater Field Copier

  • When first building sites, I use lots of placeholder data in my repeater fields so that I can quickly develop the site while not worrying too much about the final content in the spot. I have always thought the repeater could use a clone row as well as the add new row option. Basically this would work just like the “Add New” button except that it would keep all the field values of the previous repeater inside the new field.

    I feel like this is something that many devs would use a lot.

  • Hi @codydoodle

    I believe you can do it by using custom Javascript. Here’s an example how to do it:

    function my_acf_repeater_clone() {
    	
    ?>
    <script type="text/javascript">
    (function($) {
    	
        acf.add_action('append', function( el ){
    
            // $el will be equivalent to the new element being appended $('tr.row')
            
            
            // Cache the previous row first
            var prevField = el.prev();
            
            // Get the value for certain field
            var prevFieldValue = prevField.find(".acf-field-1234567890abc input").val();
            
            // Set it to the new row
            el.find(".acf-field-1234567890abc input").val(prevFieldValue);
            
    
        });
    	
    })(jQuery);	
    </script>
    <?php
    		
    }
    
    add_action('acf/input/admin_footer', 'my_acf_repeater_clone');

    Please keep in mind that you need to target the class based on the field key.

    I hope this helps 🙂

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

The topic ‘Repeater Field Copier’ is closed to new replies.