Support

Account

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

    Steps:
    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.

    Sorry I can’t supply any code, but that project died and all of the code in it would need to be rewritten to use the new ACF JS API https://www.advancedcustomfields.com/resources/javascript-api/ for the getting and setting of values as well as having all the ajax requests rebuilt.