How do i add values of input of same class. I have tried the code below but doesn't seem to work. Where am i going 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>