Sybrentjuh
Sybrentjuh

Reputation: 287

Add or substract value of mutiple elements to total value

So I have made a function which changes my values in a "cart". Every input field has their one value and this is added or substracted on it's total on change of the input field. It looks like this:

enter image description here

And my code looks like this (yes I am gonna cleanup the html+css later, this is just for testing :-):

 var extraValues = document.querySelectorAll("input[type=number]");
 var extraTotal = 0;
 
 Array.prototype.forEach.call(extraValues, function (extraValue) {
  extraValue.addEventListener('change', function() {
   var getExtraId = extraValue.id; 
   var extraLength = getExtraId.length;
   var currentExtra = getExtraId.charAt(extraLength - 1);
   var catId = "testKassabon" + currentExtra;
   var extraSort = extraValue.parentElement.querySelector('label');
   var extraPrice = extraValue.parentElement.querySelector('.extraPrice').innerText;
   var totalPrice = extraPrice * this.value;
   var showExtra = document.getElementById(catId);
   showExtra.innerHTML = "<div style='display: flex; justify-content: space-between; margin-top: 10px;'><div>" + this.value + "x " + extraSort.innerText + "</div><div style='color: #EC008C; font-weight: 700; font-size: 16px;'>€ " + totalPrice + "</div></div>";
    })
 });
.col-form-label {
 font-weight: 700;
 display: block;
 width: 100%;
 padding-bottom: 5px;
}
<div style="display: flex; justify-content: space-between; max-width: 600px;">
<div>
<div class="item" style="width: 200px; margin-bottom: 20px;">
             <span class="extraPrice" style="display: none;">7.50</span>
            <label class="col-form-label" style="font-weight: 700;">3-gangendiner</label>
            <input type="number" id="extra1" class="form-control" style="max-width: 70px;" value="0" min="0">   
</div>

<div class="item" style="width: 200px; margin-bottom: 20px;">
            <span class="extraPrice" style="display: none;">10</span>
            <label class="col-form-label">Fietshuur</label>
            <input type="number" id="extra2" class="form-control" style="max-width: 70px;" value="0" min="0">   
</div>

<div class="item" style="width: 200px; margin-bottom: 20px;">
            <span class="extraPrice" style="display: none;">2.50</span>
            <label class="col-form-label">Uitgebreid ontbijtbuffet</label>
            <input type="number" id="extra3" class="form-control" style="max-width: 70px;" value="0" min="0">   
</div>
</div>

<div style="max-width: 300px; width: 100%; padding: 20px; background-color: #F7F7F7; border-radius: 4px; margin: 0px;">
 <span style="color: #000; font-weight: 700; font-size: 20px; padding-bottom: 10px; display: block;">Uw totaalprijs</span>
 <div style="display: flex; flex-direction: column; height: 100%; padding-bottom: 40px;">
  <div id="testKassabon1"></div>
  <div id="testKassabon2"></div>
  <div id="testKassabon3" style="flex-grow: 1;"></div>
  <div id="totalPrice"></div>
 </div>
</div>
</div>

This works perfectly. But now I need to calculate the total amount of these 3 values each time one of the inputs changes, and show it in id="totalPrice". I have NO idea how to do this, since everytime I try this it just replaced the total pricing instead of adding or substracting it from the total. Does anyone know how to do this?

Upvotes: 1

Views: 83

Answers (2)

Waleed Nasir
Waleed Nasir

Reputation: 607

Try this Code man thanks :-)

  
  function countProperties(obj) {
    var count = 0;

    for(var prop in obj) {
    console.log(prop,obj)
        if(obj.hasOwnProperty(prop))
          count = count +obj[prop];
    } 

    return count;
}


var total = {}
                         var extraValues = document.querySelectorAll("input[type=number]");
                         var extraTotal = 0;
                         
                         Array.prototype.forEach.call(extraValues, function (extraValue) {
                          extraValue.addEventListener('change', function() {
                           var getExtraId = extraValue.id; 
                           var extraLength = getExtraId.length;
                           var currentExtra = getExtraId.charAt(extraLength - 1);
                           var catId = "testKassabon" + currentExtra;
                           var extraSort = extraValue.parentElement.querySelector('label');
                           var extraPrice = extraValue.parentElement.querySelector('.extraPrice').innerText;
                           var totalPrice = extraPrice * this.value;
                          total[catId] = totalPrice
                

                 // var TotalCost = Object.values(total).reduce((a, b) //=> a + b, 0)
        

   
        var TotalCost = countProperties(total)
                          console.log(TotalCost)
                         alert(TotalCost)



                           var showExtra = document.getElementById(catId);
                           showExtra.innerHTML = "<div style='display: flex; justify-content: space-between; margin-top: 10px;'><div>" + this.value + "x " + extraSort.innerText + "</div><div style='color: #EC008C; font-weight: 700; font-size: 16px;'>€ " + totalPrice + "</div></div>";
                            })
                
                
                
                         });
        .col-form-label {
                         font-weight: 700;
                         display: block;
                         width: 100%;
                         padding-bottom: 5px;
                        }
                
                        <div style="display: flex; justify-content: space-between; max-width: 600px;">
                        <div>
                        <div class="item" style="width: 200px; margin-bottom: 20px;">
                                     <span class="extraPrice" style="display: none;">7.50</span>
                                    <label class="col-form-label" style="font-weight: 700;">3-gangendiner</label>
                                    <input type="number" id="extra1" class="form-control" style="max-width: 70px;" value="0" min="0">   
                        </div>
                
                        <div class="item" style="width: 200px; margin-bottom: 20px;">
                                    <span class="extraPrice" style="display: none;">10</span>
                                    <label class="col-form-label">Fietshuur</label>
                                    <input type="number" id="extra2" class="form-control" style="max-width: 70px;" value="0" min="0">   
                        </div>
                
                        <div class="item" style="width: 200px; margin-bottom: 20px;">
                                    <span class="extraPrice" style="display: none;">2.50</span>
                                    <label class="col-form-label">Uitgebreid ontbijtbuffet</label>
                                    <input type="number" id="extra3" class="form-control" style="max-width: 70px;" value="0" min="0">   
                        </div>
                        </div>
                
                        <div style="max-width: 300px; width: 100%; padding: 20px; background-color: #F7F7F7; border-radius: 4px; margin: 0px;">
                         <span style="color: #000; font-weight: 700; font-size: 20px; padding-bottom: 10px; display: block;">Uw totaalprijs</span>
                         <div style="display: flex; flex-direction: column; height: 100%; padding-bottom: 40px;">
                          <div id="testKassabon1"></div>
                          <div id="testKassabon2"></div>
                          <div id="testKassabon3" style="flex-grow: 1;"></div>
                          <div id="totalPrice"></div>
                         </div>
                        </div>
                        </div>

Upvotes: 1

Aalexander
Aalexander

Reputation: 5004

Solution

In this solution I have added a function to calculate the sum.

So when the value changes then I call at the end of your function. The calculator which calculates the sum and insert it inside your text field.

    var extraValues = document.querySelectorAll("input[type=number]");

  //  extraValues.forEach((elem)=>{
  //  elem.addEventListener('input', function (evt) {
  //      calculator();
  //  })
  //  });
         
     Array.prototype.forEach.call(extraValues, function (extraValue) {
  extraValue.addEventListener('change', function() {
   var getExtraId = extraValue.id; 
   var extraLength = getExtraId.length;
   var currentExtra = getExtraId.charAt(extraLength - 1);
   var catId = "testKassabon" + currentExtra;
   var extraSort = extraValue.parentElement.querySelector('label');
   var extraPrice = extraValue.parentElement.querySelector('.extraPrice').innerText;
   var totalPrice = extraPrice * this.value;
   var showExtra = document.getElementById(catId);
   showExtra.innerHTML = "<div style='display: flex; justify-content: space-between; margin-top: 10px;'><div>" + this.value + "x " + extraSort.innerText + "</div><div style='color: #EC008C; font-weight: 700; font-size: 16px;'>€ " + totalPrice + "</div></div>";
   calculator()
    })
 });
     
         
    function calculator(){
    let prices = document.querySelectorAll('.extraPrice')
    let sum = 0;
    let fac = 0;
    extraValues.forEach((elem, ind) =>{
    fac = parseFloat(prices[ind].innerHTML)
    sum += parseInt(elem.value) * fac;
    })

    document.getElementById("totalPrice").innerHTML = sum;
    }
        .col-form-label {
         font-weight: 700;
         display: block;
         width: 100%;
         padding-bottom: 5px;
        }
        <div style="display: flex; justify-content: space-between; max-width: 600px;">
        <div>
        <div class="item" style="width: 200px; margin-bottom: 20px;">
                     <span class="extraPrice" style="display: none;">7.50</span>
                    <label class="col-form-label" style="font-weight: 700;">3-gangendiner</label>
                    <input type="number" id="extra1" class="form-control" style="max-width: 70px;" value="0" min="0">   
        </div>

        <div class="item" style="width: 200px; margin-bottom: 20px;">
                    <span class="extraPrice" style="display: none;">10</span>
                    <label class="col-form-label">Fietshuur</label>
                    <input type="number" id="extra2" class="form-control" style="max-width: 70px;" value="0" min="0">   
        </div>

        <div class="item" style="width: 200px; margin-bottom: 20px;">
                    <span class="extraPrice" style="display: none;">2.50</span>
                    <label class="col-form-label">Uitgebreid ontbijtbuffet</label>
                    <input type="number" id="extra3" class="form-control" style="max-width: 70px;" value="0" min="0">   
        </div>
        </div>

        <div style="max-width: 300px; width: 100%; padding: 20px; background-color: #F7F7F7; border-radius: 4px; margin: 0px;">
         <span style="color: #000; font-weight: 700; font-size: 20px; padding-bottom: 10px; display: block;">Uw totaalprijs</span>
         <div style="display: flex; flex-direction: column; height: 100%; padding-bottom: 40px;">
          <div id="testKassabon1"></div>
          <div id="testKassabon2"></div>
          <div id="testKassabon3" style="flex-grow: 1;"></div>
          <div id="totalPrice"></div>
         </div>
        </div>
        </div>

Upvotes: 1

Related Questions