I want to change the value of Total price but it keep showing me "Undefined" in Javascript

4189 views javascript
-2

So my goal is to built a shopping carte that calculate the total price of n items. I can increment/decrement the quantity of each item. So everything works except the "UpdateCartTotal" function which it shows undefined instead of the total price.How i can fix it.

    function getinput(event) {
      return event.target.parentElement.querySelector(".quantity");
    }

 // the Event Listener 
    document.addEventListener("click", function(event) {
      if (event.target.className == "plus-btn") {
        increment(event);
        updateCarteTotal(event)
      }
      if (event.target.className == "minus-btn") {
        decrement(event)
        updateCarteTotal(event)
      }
    });

 // Increment function

    function increment(event) {
      var quantity = getinput(event)
      if(quantity.value<20){
         quantity.value++
        }
    }
  // Decrement function 
    function decrement(event) {
      var quantity = getinput(event)
      if(quantity.value >=1){
         quantity.value--
        }
    }

// the function to calculate the totale Carte price

    function updateCarteTotal(event) {

        const items=document.querySelectorAll(".item");
        var total_price=document.querySelector(".total_price");
        var quantity=getinput(event);
        var unit_price=document.querySelectorAll(".price");
        var total=0;
        for(item of items ){   
            total += parseInt(quantity.value * unit_price.value)
        }
        total_price.value=total.value
    }

answered question

1 Answer

6

here total is not object

change this line total_price.value=total.value to total_price.value=total and you good to go

posted this

Have an answer?

JD

Please login first before posting an answer.