Title: Bug? Nested loops, Insert field-button
Last modified: March 9, 2024

---

# Bug? Nested loops, Insert field-button

 *  Resolved [mikaelrav](https://wordpress.org/support/users/mikaelrav/)
 * (@mikaelrav)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/bug-nested-loops-insert-field-button/)
 * I’m just starting out but I think I’ve found a bug.
   I’m using nested loops. Repeatable**
   Rows** which then contains repeatable **Tiles**.
 * When I try to add a new **row** by using the +button a **tile** is created but
   in the hierarchy of my rows.
 * **A visual demonstration**
   –Row (When I click the +button here …) –Tile –Tile–
   Tile (… a Tile is created here. It should be a Row.)
 * So basically I try to add a loop element but instead a sub-loop is inserted in
   its place.
 * It might have something to do with this code, but I’m not much of a coder myself.
 * Any suggestions?
 *     ```wp-block-code
       $(document).on('click', '.cfs_insert_field', function(event) {
   
       event.stopPropagation();
   
       var $add_field = $('.cfs_add_field');
   
       var num_rows = $add_field.attr('data-rows');
   
       var loop_tag = $add_field.attr('data-loop-tag');
   
       var loop_id = loop_tag.match(/.*\[(.*?)\]/)[1];
   
       var html = CFS.loop_buffer[loop_id].replace(/\[clone\]/g, loop_tag + '[' + num_rows + ']');
   
       $add_field.attr('data-rows', parseInt(num_rows)+1);
   
       $(html).insertAfter( $(this).closest('.loop_wrapper') ).addClass('loop_wrapper_new');
   
       $add_field.trigger('cfs/ready');
   
       });
       ```
   
    -  This topic was modified 2 years, 3 months ago by [mikaelrav](https://wordpress.org/support/users/mikaelrav/).

Viewing 1 replies (of 1 total)

 *  Thread Starter [mikaelrav](https://wordpress.org/support/users/mikaelrav/)
 * (@mikaelrav)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/bug-nested-loops-insert-field-button/#post-17488793)
 * I found a solution. I had to change a line in the code to be more specific.
 *     ```wp-block-code
       var $add_field = $('.cfs_add_field');
       ```
   
 * Had to be replaced with:
 *     ```wp-block-code
       var $add_field = $(this).parent().parent().siblings('.table_footer').find('.cfs_add_field');
       ```
   
 * I think it will then find the correct add field-button in the DOM.
    -  This reply was modified 2 years, 3 months ago by [mikaelrav](https://wordpress.org/support/users/mikaelrav/).

Viewing 1 replies (of 1 total)

The topic ‘Bug? Nested loops, Insert field-button’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-field-suite.svg)
 * [Custom Field Suite](https://wordpress.org/plugins/custom-field-suite/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-field-suite/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-field-suite/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-field-suite/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-field-suite/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [mikaelrav](https://wordpress.org/support/users/mikaelrav/)
 * Last activity: [2 years, 3 months ago](https://wordpress.org/support/topic/bug-nested-loops-insert-field-button/#post-17488793)
 * Status: resolved