I have an HTML snippet, as shown below, I want to take a div as an “article form”, clone it and increase the numbers for the attributes for, id and name.
<div class="article-form">
<label for="id_form-0-section">Section:</label>
<select id="id_form-0-section" name="form-0-section">
<option selected="selected" value="">---------</option>
<option value="1">News</option>
<option value="2">Originals</option>
</select>
<label for="id_form-0-slug">Slug:</label>
<input type="text" maxlength="255" name="form-0-slug" id="id_form-0-slug">
<input type="hidden" id="id_form-0-collection" name="form-0-collection">
<input type="hidden" id="id_form-0-order" name="form-0-order">
<input type="hidden" id="id_form-0-id" name="form-0-id">
</div>
I have the cloning part so far, but I need to take what I cloned by the intersection of elements inside, have an attribute for, id and name.
var $articeForm = $('.article-form').clone();
Let me add that part of the increment is not a problem. I plan to do the following. I am not sure what the best way to cross an attribute.
var newNumber = parseInt($('#id_form-0-id').attr('id').split('-')[1]) + 1;
One more thing: the fact that it started at 0 is pointless, in some situations it can start at 5, and then the following next fields should be 6.
source
share