0

i have a problem with the code below, I want to sum alla input values for a tr and that works good except that it show the row values on all rows instead of the changed one. Code below:

   $(".txtMult input").keyup(multInputs);

  function getNum($val) {
     if (isNaN($val)) {
       return 0;
     } else {
         return $val;
     }
  }
   function multInputs() {
       var mult = 0;
       var $total = 0;
       // for each row:
       $("tr.txtMult").each(function () {
           var $total = 0;
           $("input.val").each(function () {
           // get the values from this row:
           $total = +parseFloat($total)+getNum(parseFloat($(this).val()));
           });
           $('.multTotal',this).text($total);
       });
   }

any suggestions ?

Example on JSfiddle

6
  • 2
    Your table looks like it is working as I would expect it to. Can you explain a bit more about what exactly the problem is? Commented Dec 14, 2017 at 8:43
  • 2
    You jsFiddle code seems to be working fine. So i am unable to understand your problem. Please explain. Commented Dec 14, 2017 at 8:45
  • 1
    You want the the multiplication to be happened on the changed row, but to derive the grand total, you must go through each row and calculate it. So, your current solution is not that bad. Commented Dec 14, 2017 at 8:50
  • 1
    May be you want something else as we expected, you should explain more about that part. Commented Dec 14, 2017 at 8:51
  • Sorry guys, wrong jsfiddle code, i have update the url: jsfiddle.net/5FpWC/541 Commented Dec 14, 2017 at 9:01

1 Answer 1

1

that it show the row values on all rows instead of the changed one

The way I understand this statement is you only want to show the Total of the rows that have changed or have values, then you can just add an if/else statement like:

      function multInputs() {
           var mult = 0;
           // for each row:
           $("tr.txtMult").each(function () {
               // get the values from this row:
               var $val1 = $('.val1', this).val();
               var $val2 = $('.val2', this).val();
               var $total = ($val1 * 1) * ($val2 * 1)

               if ( $total != 0 && !isNaN($total) ) {
                   $('.multTotal',this).text($total);
                   mult += $total;
               } else $('.multTotal',this).text("");
           });
           $("#grandTotal").text(mult);
       }

Here is the fiddle: http://jsfiddle.net/5FpWC/540/

I also added a condition to only show if valid number - !isNaN($total)


Update:

Regarding the new update on this fiddle: http://jsfiddle.net/5FpWC/541/

The error is in $("input.val"), You are selecting all the input with class val on all trs.

Yous should use $(this).find("input.val") to specify that you only want the input.val on the current tr on the loop.

$("tr.txtMult").each(function () {
      var $total = 0;
     $(this).find("input.val").each(function () {
         // get the values from this row:
         $total = +parseFloat($total)+getNum(parseFloat($(this).val()));
     });
     $('.multTotal',this).text($total);
});

Fiddle: http://jsfiddle.net/5FpWC/543/

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

1 Comment

thx or the help, this works wonderful stand alone, but when i put it in to my datatables it doesnt work 100%, it does everything correct even the adding the values and the total is correct but i cant get it to write it to the span class, no error no nothing. very strange. When i alert the multirow, this i get object object. any suggestions ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.