mammam mammam - 2 months ago 7
jQuery Question

Add values in dynamic inputs using Jquery

With the help of http://www.mkyong.com/wp-content/uploads/jQuery/jQuery-add-remove-textbox.html , I got my dynamic inputs and value.

But while adding its concatenating or getting

Nan
. I need to add values.

var counter = 2;
$("#addButton").click(function () {

if(counter>20){
return false;
}

var newTextBoxDiv = $(document.createElement('div'))
.attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html(
' <div class="col-md-6 marg-top-10 "> <input type="text" class="form-control inputData" name="assetDescription' + counter +
'" id="assetDescription' + counter + '" value="" > </input> </div>' +
' <div class="col-md-6 marg-top-10 "> <input type="text" class="form-control inputData" name="textbox' + counter +
'" id="textbox' + counter + '" value="" > </input> </div>'
);

newTextBoxDiv.appendTo("#TextBoxesGroup");
counter++;
});

$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}

counter--;

$("#TextBoxDiv" + counter).remove();
});


for adding value

$("#getButtonValue").click(function () {
console.log("came ");

var msg = '';
var totalvalue;
var result;

for(i=1; i<counter; i++){
msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
result += Number($('#textbox' + i).val());

console.log(result);
}


result is =
Nan


help please!

Answer

try with this..

var result = 0;
for(i=1; i<counter; i++){
            msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
            result += Number($('#textbox' + i).val());       
                      console.log(result);
      }
Comments