-1

I can't sum on table td "jumlah" for "total"

<script>
$(document).on("change", ".jumlah", function() {
    var sum = 0;
    $(".jumlah").each(function(){
        sum += +$(this).val();
    });
    $(".total").val(sum);
});
</script>

<input type="text" id="total" class="total" value=""  />

I expect the output for total from jumlah enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

0

Elements with the [contenteditable] attribute will not have a [value] attribute because:

  1. Only non-interactive, non-void, flow and phrase elements like <div></div>, <section></section>, <spab></span> etc. can have [contenteditable].
  2. Only form controls such as <input>, <textarea>, <output>, etc. can have the [value] attribute.

So instead of .val() use .text() since text is the content between the tags of non-form controls. The important change is a ternary:

if this is an input get its value otherwise get its text

sum += $(this).is('input') ? +$(this).val() : +$(this).text(); 

The minor changes are:

  • input event instead of change because it looks slick.
  • The form is registered to the input event instead of document.

$('.totals').on("input", ".subtotal", function() {
  let sum = 0;
  $(".subtotal").each(function(){
    sum += $(this).is('input') ? +$(this).val() : +$(this).text();
  });
  $(".total").val(sum);
});
:root {font:700 16px/1.2 Consolas}
input, output, data {display:inline-block;font:inherit;text-align:right;width:125px;}
output, data {width:50px}
data {outline:1px solid #aaa;padding:2px 17px 2px 0px;margin:1px 0px 1px 2px;width:110px}
<form class='totals'>
  <table>
  <tr><td><input class="subtotal" type='number' value='0'></td></tr>
  <tr><td><input class="subtotal" type='number' value='0'></td></tr>
  <tr><td><input class="subtotal" type='number' value='0'></td></tr>
  <tr><td><data class="subtotal" contenteditable>0</data></td></tr>
  <tr><td><data class="subtotal" contenteditable>0</data></td></tr>
  <tr><td><data class="subtotal" contenteditable>0</data></td></tr>
  <tr><td>Total: <output class="total">0</output></td></tr>
  </table>
 </form>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
zer00ne
  • 41,936
  • 6
  • 41
  • 68
-1

I have created a very simple example with what you need, I think you can easily implement something similar in your code.

$('.jumlah').on('input', function() {
  let elementsToSum = $(document).find('.jumlah');
  let sum = 0;
  for (let ele of elementsToSum) {
    // Parse the value of the <td> to integer
    let eleIntValue = parseInt($(ele).html());
    if (!isNaN(eleIntValue)) {
      // Only add it to the sum if it is a number (not empty)
      sum += eleIntValue;
    }
  }
  
  // Change the value of <input> with the new calculated sum
  $('#total').val(sum);
});
td {
  width: 50px;
}
<table border="1">
  <tr>
    <td contenteditable="true" class="jumlah"></td>
  </tr>
  <tr>
    <td contenteditable="true" class="jumlah"></td>
  </tr>
  <tr>
    <td contenteditable="true" class="jumlah"></td>
  </tr>
  <tr>
    <td contenteditable="true" class="jumlah"></td>
  </tr>
</table>

<br>
<input type="text" id="total" class="total" value="" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Ali Elkhateeb
  • 3,413
  • 4
  • 21
  • 39