1

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, but still not work for barang_total value. it still empty. i'll check more detail for this barang_total
@reefman the fiddle seems to be working... can you try to recreate the issue
it seems result for one barang_total only, if there is two or more barang_total row, value wiil be same
1

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); 
}); 

Comments

1

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

2 Comments

Please access demo link to see result. If you have any question, you need a demo to show your code. Anyone can help when they reviewed it. :)
thank you it is my fault ,, yes i missed the id for input has name barang_total[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.