I am trying to write some simple HTML/jquery that will apply the same calculation to different fields.
I have simplified the problem down to the following HTML:
<h1>Multiplication by line</h1>
<table>
<tr>
<td>Number 1</td>
<td>Number 2</td>
<td>Result</td>
</tr>
<!-- LINE ITEM START -->
<div class="lineitem">
<tr>
<td>
<input type="number" name="a" class="a" value="" />
</td>
<td>
<input type="number" name="b" class="b" value="" />
</td>
<td>
<div class="total">0 </div>
</td>
</tr>
</div>
<!-- LINE ITEM END -->
<div class="lineitem">
<tr>
<td>
<input type="number" name="a" class="a" value="" />
</td>
<td>
<input type="number" name="b" class="b" value="" />
</td>
<td>
<div class="total">0</div>
</td>
</tr>
</div>
And Jquery:
$(document).ready(function() {
$(".lineitem").each(function() {
function compute() {
var a = $('.a').val();
var b = $('.b').val();
var total = (a * b);
$('.total').text(total);
}
$('.b, .a').change(compute);
})
});
But this returns the same Result for each line based on the first line's data entry. I obviously want the user to put in different values in each line and get a different result per line. I tried using "each" and "$(this).children" as follows, but it didn't work:
$(document).ready(function() {
$(".lineitem").each(function () {
function compute () {
var a = $(this).children('.a').val();
var b = $(this).children('.b').val();
var total = (a*b);
$(this).children('.total').text(total);
}
$('.b, .a').change(compute);
})}); */
Here's a JSFiddle with everything.
Really hope someone can help! I've done a fair bit of searching but no joy on the basic question here...apologies if it's a repeat
tris enclosed in adiv?