Edyl I. Templado
Edyl I. Templado

Reputation: 63

How To get multiple array and change value of its related array key using Jquery

This is the sample image of the output. What I want is if I change the quantity in that column its co-related total-price will change also. I want to achieve this injquery yet Im struggling.This is my current code and it still has error if i update the quantity all the total price will change which is wrong I want to only change the total price of the edited column.

var delay = (function(){
    var timer = 0;
    return function(callback, ms){
      clearTimeout (timer);
      timer = setTimeout(callback, ms);
    };
  })();

  $(".qty-mod").keyup(function(){
          var qty = $(this).val();
          delay(function(){
          var newprice = qty * totalorgprice
          $(".price-total").val(newprice);
          console.log(qty);
      }, 1000);
  });  

enter image description here

Upvotes: 0

Views: 56

Answers (1)

cFreed
cFreed

Reputation: 4484

You've bound all .qty-mod at once, and this is good.

But when in the keyup() function you're operating on only one .qty-mod, so should update only the corresponding .price-total.
And currently you're explicitly updating all of them!

  $(".qty-mod").keyup(function(){
      var qty = $(this).val();
      var newprice = qty * totalorgprice;
      $(<< here should refer to the involved .price-total only >>).val(newprice);
      console.log(qty);
  });

Since you didn't show your HTML code I can only guess that your data is displayed in a <table>. If so, the << here ... only >> above might be something like:

'.price-total', $(this).closest('tr')

Explained for the case you're not easy with jQuery:

  '.price-total',    // an element with class "price-total"
  $(this)            // in the context of 
    .closest('tr')   // the <tr> parent of the [$(this)] current element

Upvotes: 1

Related Questions