I need to write a script to calculate working hours. I have a table with 6 possible amounts of hours which need to be added together in a field at the bottom.
The HTML looks like this:
<table>
<tr>
<td><input type="text" name="amount[]" class="countAmount"></td>
<td>(...)</td>
</tr>
<tr>
<td><input type="text" name="amount[]" class="countAmount"></td>
<td>(...)</td>
</tr>
<tr>
<td><input type="text" name="amount[]" class="countAmount"></td>
<td>(...)</td>
</tr>
<tr>
<td>Total: <span id="total"></span></td>
</tr>
</table>
I am trying to get the values of the class countAmount counted together.
I have no idea where to start with the jQuery code. Everything I tried was useless, but the main problems are how to handle the empty values and how to handle the values as an INT.
To find the inputs:
To convert to numbers and handle blanks:
To loop through them, use
each.So:
…since within the loop,
thiswill be theinput, and to get the input’s value, you don’t need jQuery, thevalueproperty is fine.Then to put it in your
totalspan: