How can I dynamically create a form element and provide it with dynamically incremented input name attributes? I'm working with jquery but am open to any solutions.
I have a div that looks like this:
<div>
<input type="text" name="title1" />
<input type="text" name="body1" />
<select name="photo1" size="1">
<option value="0">foo</option>
</select>
</div>
and I want to clone it n times AND increment the name values by 1 so, for example, a second div would look like this (only the name values change):
<div>
<input type="text" name="title2" />
<input type="text" name="body2" />
<select name="photo2" size="1">
<option value="0">foo</option>
</select>
</div>
Cloning a div is simple with jquery:
$(document).ready(function(){ $('.toClone').click(function(){ $(this).clone(true).insertAfter(this); }); });
but I am having trouble automatically incrementing the name value of the dynamically created div fields.
Anyone have insight into how to do this?