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?
cd? | Function name should beempty()