I am trying to increment a value of an hidden input and increment an index of an input name. I found a solution to increment the index but, I don't know how to increment the value of the hidden input.
Here my HTML
<table class='table-results'>
<tr>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
<tr>
<td>
<input name="result[1][key]" type="hidden" value="A">
<input placeholder="Title*" class="form-control result-title" name="result[1][title]" type="text">
</td>
<td>
<textarea placeholder="Description*" class="form-control dv-textarea" name="result[1][description]" cols="50" rows="10"></textarea>
</td>
<td>
<input type='button' class='add-question' value='add'>
</td>
</tr>
</table>
And the JS
$('.table-results').on('click', '.add-question',function(event){
event.preventDefault();
var selector = $(this).closest('tr').find("input[type=hidden]").attr('name');
var index_value = selector.match(/\d+/)[0];
var nextIndexValue = parseInt(index_value,10)+1;
var thisRow = $(this).closest('tr');
$(thisRow).clone().insertAfter(thisRow).find('input, textarea').each(function(){
this.name = this.name.replace(selector.match(/\d+/)[0],nextIndexValue)
})
});
Expected :
<table class='table-results'>
<tr>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
<tr>
<td>
<input name="result[1][key]" type="hidden" value="A">
<input placeholder="Title*" class="form-control result-title" name="result[1][title]" type="text">
</td>
<td>
<textarea placeholder="Description*" class="form-control dv-textarea" name="result[1][description]" cols="50" rows="10"></textarea>
</td>
<td>
<input type='button' class='add-question' value='add'>
</td>
</tr>
<tr>
<td>
<input name="result[2][key]" type="hidden" value="B">
<input placeholder="Title*" class="form-control result-title" name="result[2][title]" type="text">
</td>
<td>
<textarea placeholder="Description*" class="form-control dv-textarea" name="result[2][description]" cols="50" rows="10"></textarea>
</td>
<td>
<input type='button' class='add-question' value='add'>
</td>
</tr>
</table>
Jsfiddle