0

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!

2
  • I test your link, but here are everything is ok. see screenshot: prntscr.com/ck769b Commented Sep 20, 2016 at 12:28
  • You can use parseInt instead of Number as well. Commented Sep 20, 2016 at 12:32

1 Answer 1

1

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);
      }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sarath!!
@Loading.. simply initialised the variable as number :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.