I have unknown number of input fields, having class “add” I just wants to sum these with jquery, dont know where I am wrong.
<input name="add" class="add" type="text">
<input name="add" class="add" type="text">
<input name="add" class="add" type="text">
<input name="add" class="add" type="text">
<input type="button" value="" onClick="add()" />
`
function add(){
val = 0;
$(".add").each(function() {
str = (parseInt(this.value))
sum=str+str
});
alert (sum)
}
`
You’re never actually adding stuff into
sum:If the intention is to only support whole numbers, use
parseInt(this.value, 10)[note the radix parameter] instead of+this.value:See http://jsfiddle.net/alnitak/eHsJP/