I have a table where each row is a separate form:
<tr>
<form method="post" action="/items/1/edit">
<td><input type="number" id="duration[1]" onChange="calculateTotal(this);" value="10.0"></td>
<td><input type="number" id="rate[1]" onChange="calculateTotal(this);" value="25.0"></td>
<td><input disabled type="number" id="total[1]" value="250.0"></td>
</form>
</tr>
<tr>
<form method="post" action="/items/2/edit">
<td><input type="number" id="duration[2]" onChange="calculateTotal(this);" value="20.0"></td>
<td><input type="number" id="rate[2]" onChange="calculateTotal(this);" value="50.0"></td>
<td><input disabled type="number" id="total[2]" value="1000.0"></td>
</form>
</tr>
I'd like to have the value of the total input be calculated when a change is made to the duration or rate input in the same row.
My "hard coded" function works for the first row only:
function calculateTotal(sender) {
// use sender.closest('');
var durationElement = document.getElementById("duration[1]");
var durationValue = durationElement.value;
var rateElement = document.getElementById('rate[1]');
var rateValue = rateElement.value;
if ( durationValue && rateValue)
{
var totalValue = durationValue * rateValue;
var durationElement = document.getElementById("total[1]");
durationElement.value = totalValue;
}
}
I think the element.closest() function is the right approach, but I can't get it to work.
How do I adjust it to make it based on the row of the item being edited?