I have a user case scenario when there a structure like there is a parent div with class 'row'. Within that div 2 child divs are present. There is a 'Add more' link. Whenever user click one that link the whole 'inner divs' structure will append. It can happen multiple times. Now the requirement is I have to send all the values to server within a array where multiple object will hold the value of all the max and min field values. Now the problem I am currently facing is, I am only able to push the last element values in to array. Below is my javascript code
I have tried with jQuery and JavaScript. Both times I was unable to produce desired result.
var count = 0;
$('#addNew').on('click', function() {
count++;
var innerContent = `<div class='col-xs-6'><input type='text' id='minval'` + count + ` /></div><div class='col-xs-6'><input type='text' id='maxval'` + count + ` /></div>`
$('.row').append(innerContent);
});
$('#save').on('click', function() {
var value = [];
for (var i = 0; i <= count; i++) {
value.push({
minval: $('#minval' + i).val(),
maxval: $('#maxval' + i).val(),
});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='row'>
<div class='col-xs-6'><input type='text' id='minval0' /></div>
<div class='col-xs-6'><input type='text' id='maxval0' /></div>
</div>
<button id='addNew'>Add More</button>
I expect the output of value
[{minval:1,maxval:2},{minval:2,maxval:3},{minval:3,maxval:4}]