I have the following table:
<table>
<thead>
<tr>
<th></th>
<th>Female</th>
<th>Male</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rising 1</td>
<td>
<input id="firstinput" />
</td>
<td>
<input id="secondinput" />
</td>
</tr>
<tr>
<td>Rising 2</td>
<td>
<input id="thirdinput" />
</td>
<td>
<input id="fourthinput" />
</td>
</tr>
<tr>
<td>2+</td>
<td>
<input id="fifthinput" />
</td>
<td>
<input id="sixthinput" />
</td>
</tr>
</tbody>
</table>
I'm wanting to add all the values in these inputs together and display their value:
$(document).ready(function () {
$('#mybutton').click(function () {
alert(parseInt($('#firstinput').val()) +
parseInt($('#secondinput').val()) +
parseInt($('#thirdinput).val()) +
parseInt($('#fourthinput').val()) +
parseInt($('#fifthinput).val()) +
parseInt($('#sixthinput').val()));
});
});
Well this only works when all the values are present in the table. If one is empty end up with NaN.
How do you get around this?
Also am I approaching this completely wrong? Is there a better way to achieve this?