Can anyone help me figure out how to sum the values of input fields with the same class? I've attempted the code below but it doesn't seem to be working. Any ideas on what I'm doing wrong?
<table id="tableID">
<tr>
<td> <input name="name" class="compulsory1" type="text" value="1" /> </td>
<td> <input name="name1" class="compulsory1" type="text" value="2" /> </td>
<td> <input name="name2" class="compulsory1" type="text" value="3" /> </td>
<td>
<script type="text/javascript">
var tdsCompulsory = document.getElementsByClassName('compulsory1')[0].value;
var cData = [];
sum = 0;
for(var i in tdsCompulsory){
if(typeof tdsCompulsory[i].textContent != 'undefined')
cData.push(tdsCompulsory[i].textContent);
}
console.log(cData);
for(var i in cData){
sum +=parseInt(cData[i]);
}
alert (sum);
</script>
</td>
</tr>
</table>