0

Below Example is used to add numbers in the text boxes added dynamically.
First Input tells that the number of text box should be displayed. If its 5 then 5 text box will be displayed.

HTML

<div id="maindiv">
Enter Count <input name="test" id="cd"/>
<input type="button" value="GetValue" id="btn"/>
<input name="test1"  class ="test" id="cd"/>
</div>

jQuery

$(document).on('click', "#btn", function(){  
    $('#maindiv').empty;
    var count=$('#cd').val();
    var i=0;
    while(i<count) {
        $('#maindiv').append("<div><input name=DynamicTextBox  type=text          class=test/></div>");              
    } 
    $('#maindiv').append("<input type=button value=add  class=btn1 />");                
});

it gives output when I use input name -Shows correct answer. I used latest version of jquery. thats why I use document.on

$(document).on('click', ".btn1", function(){  
    var sum=0;
    var d;
    $("input[name=DynamicTextBox]").each(function () {
        d=$(this).val();
        sum=sum+parseInt(d);                
    });
    alert(sum);
});

But When I use class name it shows wrong answer .

 $(document).on('click', ".btn1", function(){  
    var sum=0;
    var d;
    $(".test").each(function () {
        d=$(this).val();
        sum=sum+parseInt(d);
    });
    alert(sum);
 });

Why classname.each function is not working for dynamically added elemenets. but if I add any text box(Not Dynamically), classname.each function is working in that case. Is there any method for dynamically added class?

2
  • Two elements with same ID cd? | Function name should be empty() Commented Apr 9, 2015 at 5:05
  • Same ID cd does make any effect on answer Commented Apr 9, 2015 at 5:16

1 Answer 1

0

You didn't put a space after class=test, so the class is test/. The best practice is to quote your attributes.

$(document).on('click', "#btn", function() {
  $('#maindiv').empty;
  var count = $('#cd').val();
  var i = 0;
  while (i < count) {
    $('#maindiv').append('<div><input name="DynamicTextBox"  type="text" class="test" /></div>');
    i++;
  }
  $('#maindiv').append('<input type="button" value="add"  class="btn1" />');
});

$(document).on('click', ".btn1", function() {
  var sum = 0;
  var d;
  $(".test").each(function() {
    d = $(this).val();
    sum = sum + parseInt(d);
  });
  alert(sum);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="maindiv">
  Enter Count
  <input name="test" id="cd" />
  <input type="button" value="GetValue" id="btn" />
  <input name="test1" class="test" id="cd" />
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

.test includes the input field to the right of the button, did you remember to fill that in?
You should probably check whether parseInt returns NaN. Otherwise, it only works if all the fields are filled in.

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.