Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello
I am coding an online-shopping system Cart with JavaScript. my problem is to update Total Price, when the product quantity changes, I don Not know how to do that. related codes are:

HTML:

HTML
<div class="product-price" style="margin-left:60px;"></div>

<div class="quantity">
  <button class="plus-btn" type="button" name="button">
      <img src="../img/svg/PlusQ.png" alt="" />
  </button>
   
  <input type="text" name="name" value="1">

  <button class="minus-btn" type="button" name="button">
    <img src="../img/svg/MinusQ.png" alt="" />
  </button>
                            
<div class="total-price">&lt;label>&lt;/label></div>





JS:
JavaScript
$('.minus-btn').on('click', function (e) {
    e.preventDefault();
    var $this = $(this);
    var $input = $this.closest('div').find('input');
    var $totalprice = $this.closest('div').find('lable'); // I have problem with this line
    var value = parseInt($input.val());

    if (value != 1) {
        value = value - 1;
    } else {
        value = 0;
    }

    $input.val(value);
    $totalprice.innerHTML = value * 2; // Testing if Total price works, It Does NOT

});

$('.plus-btn').on('click', function (e) {
    e.preventDefault();
    var $this = $(this);
    var $input = $this.closest('div').find('input');
    var value = parseInt($input.val());

    if (value != 100) {
        value = value + 1;
    } else {
        value = 100;
    }

    $input.val(value);
});


What I have tried:

in fact, I do not know how to access .product-price and .total-price that are a "div" element before and after the current "div".
Posted
Updated 28-Nov-16 6:10am
v2

1 solution

Look at your markup and look at your jquery. Do you have an element called "lable" inside the div? No, so $totalprice doesn't refer to anything. Given your div has a class you should use that to find it

$totalprice = $(".total-price");
 
Share this answer
 
Comments
Ali Majed HA 28-Nov-16 11:18am     CRLF
Hello but does it refer to closest .total-price ? I have a dive with class .total-price in each row.
F-ES Sitecore 28-Nov-16 12:06pm    
No, that code will get all total-price divs, but you can use $this.closest(".total-price") if that suits your needs better.
Ali Majed HA 28-Nov-16 12:16pm    
thanks for your answer, but it does not work. I think because $this is inside a "div" element and .total-price is outside that "div" element

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900