Reputation: 143
I want to set another element value in element each function. my script (JS):
function getTotal() { $(':input[id^="barang_qty[]"]').keyup(function() {
var total = 0;
var $inputs = $(':input[id^="barang_qty[]"]');
$inputs.each(function (index) { total += parseFloat($(this).val()) * parseFloat($(this).attr('data-price'));
$("#barang_total[]").val(total);
});
$('#total').val(total);
});
HTML :
<td class="price">
<input type="text" name="barang_unit_price[]" readonly id="barang_unit_price[]" class="form-control">
</td>
<td>
<input type="text" name="barang_qty[]" id="barang_qty[]" onkeyup="getTotal()" class="form-control" >
</td>
<td>
<input type="text" name="barang_total[]" class="sum form-control" >
</td>
but element barang_total still empty.
Regards, Thank You
Upvotes: 1
Views: 428
Reputation: 388316
The problem is you are adding a keyup handler in the getTotal
method, so the first key up does not do any calculation.
Instead either you can do the calculation in the getTotal() method, with a inline call
function getTotal() {
var total = 0;
var $inputs = $('input[name="barang_qty[]"]');
$inputs.each(function(index) {
var sum = parseFloat($(this).val()) * parseFloat($(this).attr('data-price')) || 0;
total += sum;
$(this).closest('tr').find('[name="barang_total[]"]').val(sum);
});
$('#total').val(total);
}
Or use dom ready handler to register the event handler instead of using inline event handlers.
<input type="text" name="barang_qty[]" id="barang_qty[]" class="form-control" >
then
jQuery(function() {
$('input[name="barang_qty[]"]').keyup(function() {
var total = 0;
var $inputs = $('input[name="barang_qty[]"]');
$inputs.each(function(index) {
var sum = parseFloat($(this).val()) * parseFloat($(this).attr('data-price')) || 0;
total += sum;
$(this).closest('tr').find('[name="barang_total[]"]').val(sum);
});
$('#total').val(total);
});
})
Note: If you are dealing with dynamic elements then you will have to use event delegation
Demo: Fiddle
Upvotes: 1
Reputation: 637
You're missed the id for input has name barang_total[]
.
Try this, (i've tested in jsbin, it's worked)
<td class="price">
<input type="text" name="barang_unit_price[]" readonly id="barang_unit_price[]" class="form-control" value="100">
</td>
<td>
<input type="text" name="barang_qty[]" data-price="100" id="barang_qty[]" onkeyup="getTotal()" class="form-control" value="2">
</td>
<td>
<input type="text" name="barang_total[]" id="barang_total[]" class="sum form-control" >
</td>
JS
function getTotal() {
$(':input[id^="barang_qty[]"]').keyup(function() {
var total = 0;
var $inputs = $(':input[id^="barang_qty[]"]');
$inputs.each(function (index) {
total += parseFloat($(this).val()) * parseFloat($(this).attr('data-price'));
$(':input[id^="barang_total[]"]').val(total); //edited this line
});
$('#total').val(total);
});
}
Demo: http://jsbin.com/kijifisinu/edit?html,js,output
Upvotes: 1
Reputation: 9
try this
function getTotal() { $(':input[id^="barang_qty[]"]').keyup(function() {
var total = 0;
var $inputs = $(':input[id^="barang_qty[]"]');
$inputs.each(function (index) { total += parseFloat($(this).val()) * parseFloat($(this).attr('data-price'));
(function (total) {
$("#barang_total[]").val(total);
})(total);
});
$('#total').val(total);
});
Upvotes: 1