I have a table with two inputs per row and I am trying to calculate the difference between each input for that row and display it in the 3rd column.
I have looked around and tried answers from Here, here, here and here as well as others and none seem to work for me.
$(".calibration_input_lin").blur(function(){
var input = $(this)
var val = input.val()
var row = input.parents('tr').eq(0)
var req = input.closest('td').prev().val()
var res = $(".resolution").data("resolution")
var diff = difference = val - req
var diff = diff.toFixed(res)
$.ajax({
url: "<%= customer_asset_calibration_header_path(@customer,@asset,@calibration_header) %>",
data: { value: val }
}).done(function( response ) {
row.find(".calibration_lin_input_diff").text(diff)
window.alert(req);
});
// or you can run some JS code here to calculate difference
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-hover table-sm linearity">
<thead>
<tr>
<th>Weights</th>
<th>Required</th>
<th>Actual</th>
<th>Difference</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td class="calibration_input_req"><input step="any" required="required" value="0.000" class="form-control calibration_input_lin" type="number" name="result_id[2677][required]" id="result_id_2677_required" /></td>
<td><input step="any" required="required" id="linearity_actual" class="form-control calibration_input_lin" type="number" name="result_id[2677][actual]" /></td>
<td> class ="calibration_lin_input_diff"></td>
</tr>
<tr>
<td></td>
<td class="calibration_input_req"><input step="any" required="required" value="0.005" class="form-control calibration_input_lin" type="number" name="result_id[2678][required]" id="result_id_2678_required" /></td>
<td><input step="any" required="required" id="linearity_actual" class="form-control calibration_input_lin" type="number" name="result_id[2678][actual]" /></td>
<td> class ="calibration_lin_input_diff"></td>
</tr>
<tr>
<td></td>
<td class="calibration_input_req"><input step="any" required="required" value="0.050" class="form-control calibration_input_lin" type="number" name="result_id[2679][required]" id="result_id_2679_required" /></td>
<td><input step="any" required="required" id="linearity_actual" class="form-control calibration_input_lin" type="number" name="result_id[2679][actual]" /></td>
<td> class ="calibration_lin_input_diff"></td>
</tr>
</tbody>
<table>
I use the same script elsewhere and it works fine when the req
variable is static accross all table rows. I just can't seem to get it to pick up the input in td 2.
Any help would be appreciated.