I have a script to do some calculations however the target fields are in a repeater.
How can I make my script target the class in the same row?
I have been trying jquery .closest()
but with little success.
REPEATER OUTPUT
<table>
<tr>
<td>
<input class="Time1" value="10:00" />
</td>
<td>
<input class="Time2" value="12:00" />
</td>
<td>
<input class="Hours" value="0" />
</td>
</tr>
</table>
<table>
<tr>
<td>
<input class="Time1" value="10:00" />
</td>
<td>
<input class="Time2" value="12:00" />
</td>
<td>
<input class="Hours" value="0" />
</td>
</tr>
</table>
SCRIPT
$(function () {
function calculate() {
var time1, time2, hours1, hours2, mins1, mins2, hours, mins;
time1 = $(".Time1").val().split(':'),
time2 = $(".Time2").val().split(':'),
hours1 = parseInt(time1[0], 10),
hours2 = parseInt(time2[0], 10),
mins1 = parseInt(time1[1], 10),
mins2 = parseInt(time2[1], 10),
hours = hours2 - hours1,
mins = 0;
if (hours < 0) hours = 24 + hours;
if (mins2 >= mins1) {
mins = mins2 - mins1;
}
else {
mins = (mins2 + 60) - mins1;
hours--;
}
mins = mins / 60; // take percentage in 60
hours += mins;
hours = hours.toFixed(2);
$(".Hours").val(hours);
}
$(".Time1,.Time2").change(calculate);
calculate();
});
JSFIDDLE
In this example when you update a time in row one the last column in both rows will update. However when you update row two nothing.