Home › Forums › Add-ons › Repeater Field › Add row to repeater field with js? › Reply To: Add row to repeater field with js?
I still have that code, but none of it will work any more because of the new ACF JS API.
Basically you need to add an event to something, when that happens I just trigger the add row link for the repeater. In my case it’s something like
$('[data-key="field_56f3e299b6407"] ul.acf-actions a[data-event="add-row"]').trigger('click');
field_56f3e299b6407 is the repeater key.
You can trigger the removal of a row using
$('[data-key="field_56f3e299b6407"] tr a[data-event="remove-row"]').not('[data-key="field_56f3e299b6407"] tr.acf-clone a[data-event="remove-row"]').trigger('click');
The actual selectors may be different depending on your repeater layout.
If you’re going to remove and add multiple rows to the repeater you need to be careful of the order that you do things because the rows you want to remove may still be there when you start adding data. There is a delay in the removal
1) Remove repeater rows
2) Add enough repeater rows to hold the data you want to add
3) Add the data to sub fields starting at the end and moving up/backwards. This is the important part because the fields you removed will still be there until your script ends and the DOM refreshes.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.