I have a form like this:
<form>
<div class="repeatable">
<div class="repeatable">
<div class="repeatable">
<input name="level_three">
<input name="inner_three">
<a>+</a>
</div>
<input name="level_two">
<a>+</a>
</div>
<input name="level_one">
<a>+</a>
</div>
</form>
The plus sign clones the entire repeatable closest to itself. I want a jQuery function to iterates through repeatables and make the input name's an array like this:
<form>
<div class="repeatable">
<div class="repeatable">
<div class="repeatable">
<input name="level_three[0][0]">
<input name="inner_three[0][0]">
<a>+</a>
</div>
<div class="repeatable">
<input name="level_three[0][1]">
<input name="inner_three[0][1]">
<a>+</a>
</div>
<input name="level_two[0]">
<a>+</a>
</div>
<div class="repeatable">
<div class="repeatable">
<input name="level_three[1][0]">
<input name="inner_three[1][0]">
<a>+</a>
</div>
<div class="repeatable">
<input name="level_three[1][1]">
<input name="inner_three[1][1]">
<a>+</a>
</div>
<div class="repeatable">
<input name="level_three[1][2]">
<input name="inner_three[1][2]">
<a>+</a>
</div>
<input name="level_two[1]">
<a>+</a>
</div>
<input name="level_one">
<a>+</a>
</div>
</form>
We may have any number of repeatables. In the example there are 3 levels total. It may 1, 2, 3, 4 or any number.
see code example here https://jsbin.com/colocunoqo/edit?html,js,output
p.s. any templating lib like angular would make this super easy for you
EDIT: with recursion https://jsbin.com/sisivucaxe/edit?html,js,output