calculate the sum of shopping cart












0















I want to calculate the total(sum) of all the purchases using the "+","-" to add and remove products from the shopping cart.It should display the amount on the left top corner in front of "total:".In front of each product, the total purchase of the single purchase is displayed.



Here a descriptive image



Here the html code:



<body>
<div class="shopping-cart">
<!-- Title -->
<div class="title">
Shopping Bag total :
</div>

<!-- Product #1 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="item-1.png" alt="" />
</div>

<div class="description">
<span>Common Projects</span>
<span>Bball High</span>
<span>White</span>
</div>

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

</div>

<div class="total-price" id="549"><input type="number" value="0" class="result-btn" /></div>

</div>

<!-- Product #2 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="item-2.png" alt=""/>
</div>

<div class="description">
<span>Maison Margiela</span>
<span>Future Sneakers</span>
<span>White</span>
</div>

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

<div class="total-price" id="270"><input type="number" value="0" class="result-btn" /></div>
</div>

<!-- Product #3 -->
<div class="item">
<div class="buttons">
<span class="delete-btn"></span>
<span class="like-btn"></span>
</div>

<div class="image">
<img src="item-3.png" alt="" />
</div>

<div class="description">
<span>Our Legacy</span>
<span>Brushed Scarf</span>
<span>Brown</span>
</div>

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

<div class="total-price" id="349"><input type="number" value="0" class="result-btn" /></div>
</div>
</div>
</body>


Here the JS code:



const items = document.querySelectorAll('.item');

items.forEach(function(item) {
const minusButton = item.querySelector('.minus-btn');
const plusButton = item.querySelector('.plus-btn');
const inputField = item.querySelector('.input-btn');
const resultField = item.querySelector('.result-btn');

minusButton.addEventListener('click', function minusProduct() {
const currentValue = Number(inputField.value);
if (currentValue > 0) {
inputField.value = currentValue - 1;
var x = item.querySelector('.total-price').id;
resultField = item.querySelector('.result-btn').value = x* inputField.value;
} else inputField.value = 0
});

plusButton.addEventListener('click', function plusProduct() {
const currentValue = Number(inputField.value);
if (currentValue < 100) {
inputField.value = currentValue + 1;
var x = item.querySelector('.total-price').id;
resultField = item.querySelector('.result-btn').value = x* inputField.value;
document.querySelector('.title').innerHTML = item.querySelector('.result-btn')[0].value
+item.querySelector('.result-btn')[1].value + item.querySelector('.result-btn')[2].value
} else inputField.value = 100
});

});









share|improve this question





























    0















    I want to calculate the total(sum) of all the purchases using the "+","-" to add and remove products from the shopping cart.It should display the amount on the left top corner in front of "total:".In front of each product, the total purchase of the single purchase is displayed.



    Here a descriptive image



    Here the html code:



    <body>
    <div class="shopping-cart">
    <!-- Title -->
    <div class="title">
    Shopping Bag total :
    </div>

    <!-- Product #1 -->
    <div class="item">
    <div class="buttons">
    <span class="delete-btn"></span>
    <span class="like-btn"></span>
    </div>

    <div class="image">
    <img src="item-1.png" alt="" />
    </div>

    <div class="description">
    <span>Common Projects</span>
    <span>Bball High</span>
    <span>White</span>
    </div>

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

    </div>

    <div class="total-price" id="549"><input type="number" value="0" class="result-btn" /></div>

    </div>

    <!-- Product #2 -->
    <div class="item">
    <div class="buttons">
    <span class="delete-btn"></span>
    <span class="like-btn"></span>
    </div>

    <div class="image">
    <img src="item-2.png" alt=""/>
    </div>

    <div class="description">
    <span>Maison Margiela</span>
    <span>Future Sneakers</span>
    <span>White</span>
    </div>

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

    <div class="total-price" id="270"><input type="number" value="0" class="result-btn" /></div>
    </div>

    <!-- Product #3 -->
    <div class="item">
    <div class="buttons">
    <span class="delete-btn"></span>
    <span class="like-btn"></span>
    </div>

    <div class="image">
    <img src="item-3.png" alt="" />
    </div>

    <div class="description">
    <span>Our Legacy</span>
    <span>Brushed Scarf</span>
    <span>Brown</span>
    </div>

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

    <div class="total-price" id="349"><input type="number" value="0" class="result-btn" /></div>
    </div>
    </div>
    </body>


    Here the JS code:



    const items = document.querySelectorAll('.item');

    items.forEach(function(item) {
    const minusButton = item.querySelector('.minus-btn');
    const plusButton = item.querySelector('.plus-btn');
    const inputField = item.querySelector('.input-btn');
    const resultField = item.querySelector('.result-btn');

    minusButton.addEventListener('click', function minusProduct() {
    const currentValue = Number(inputField.value);
    if (currentValue > 0) {
    inputField.value = currentValue - 1;
    var x = item.querySelector('.total-price').id;
    resultField = item.querySelector('.result-btn').value = x* inputField.value;
    } else inputField.value = 0
    });

    plusButton.addEventListener('click', function plusProduct() {
    const currentValue = Number(inputField.value);
    if (currentValue < 100) {
    inputField.value = currentValue + 1;
    var x = item.querySelector('.total-price').id;
    resultField = item.querySelector('.result-btn').value = x* inputField.value;
    document.querySelector('.title').innerHTML = item.querySelector('.result-btn')[0].value
    +item.querySelector('.result-btn')[1].value + item.querySelector('.result-btn')[2].value
    } else inputField.value = 100
    });

    });









    share|improve this question



























      0












      0








      0








      I want to calculate the total(sum) of all the purchases using the "+","-" to add and remove products from the shopping cart.It should display the amount on the left top corner in front of "total:".In front of each product, the total purchase of the single purchase is displayed.



      Here a descriptive image



      Here the html code:



      <body>
      <div class="shopping-cart">
      <!-- Title -->
      <div class="title">
      Shopping Bag total :
      </div>

      <!-- Product #1 -->
      <div class="item">
      <div class="buttons">
      <span class="delete-btn"></span>
      <span class="like-btn"></span>
      </div>

      <div class="image">
      <img src="item-1.png" alt="" />
      </div>

      <div class="description">
      <span>Common Projects</span>
      <span>Bball High</span>
      <span>White</span>
      </div>

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

      </div>

      <div class="total-price" id="549"><input type="number" value="0" class="result-btn" /></div>

      </div>

      <!-- Product #2 -->
      <div class="item">
      <div class="buttons">
      <span class="delete-btn"></span>
      <span class="like-btn"></span>
      </div>

      <div class="image">
      <img src="item-2.png" alt=""/>
      </div>

      <div class="description">
      <span>Maison Margiela</span>
      <span>Future Sneakers</span>
      <span>White</span>
      </div>

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

      <div class="total-price" id="270"><input type="number" value="0" class="result-btn" /></div>
      </div>

      <!-- Product #3 -->
      <div class="item">
      <div class="buttons">
      <span class="delete-btn"></span>
      <span class="like-btn"></span>
      </div>

      <div class="image">
      <img src="item-3.png" alt="" />
      </div>

      <div class="description">
      <span>Our Legacy</span>
      <span>Brushed Scarf</span>
      <span>Brown</span>
      </div>

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

      <div class="total-price" id="349"><input type="number" value="0" class="result-btn" /></div>
      </div>
      </div>
      </body>


      Here the JS code:



      const items = document.querySelectorAll('.item');

      items.forEach(function(item) {
      const minusButton = item.querySelector('.minus-btn');
      const plusButton = item.querySelector('.plus-btn');
      const inputField = item.querySelector('.input-btn');
      const resultField = item.querySelector('.result-btn');

      minusButton.addEventListener('click', function minusProduct() {
      const currentValue = Number(inputField.value);
      if (currentValue > 0) {
      inputField.value = currentValue - 1;
      var x = item.querySelector('.total-price').id;
      resultField = item.querySelector('.result-btn').value = x* inputField.value;
      } else inputField.value = 0
      });

      plusButton.addEventListener('click', function plusProduct() {
      const currentValue = Number(inputField.value);
      if (currentValue < 100) {
      inputField.value = currentValue + 1;
      var x = item.querySelector('.total-price').id;
      resultField = item.querySelector('.result-btn').value = x* inputField.value;
      document.querySelector('.title').innerHTML = item.querySelector('.result-btn')[0].value
      +item.querySelector('.result-btn')[1].value + item.querySelector('.result-btn')[2].value
      } else inputField.value = 100
      });

      });









      share|improve this question
















      I want to calculate the total(sum) of all the purchases using the "+","-" to add and remove products from the shopping cart.It should display the amount on the left top corner in front of "total:".In front of each product, the total purchase of the single purchase is displayed.



      Here a descriptive image



      Here the html code:



      <body>
      <div class="shopping-cart">
      <!-- Title -->
      <div class="title">
      Shopping Bag total :
      </div>

      <!-- Product #1 -->
      <div class="item">
      <div class="buttons">
      <span class="delete-btn"></span>
      <span class="like-btn"></span>
      </div>

      <div class="image">
      <img src="item-1.png" alt="" />
      </div>

      <div class="description">
      <span>Common Projects</span>
      <span>Bball High</span>
      <span>White</span>
      </div>

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

      </div>

      <div class="total-price" id="549"><input type="number" value="0" class="result-btn" /></div>

      </div>

      <!-- Product #2 -->
      <div class="item">
      <div class="buttons">
      <span class="delete-btn"></span>
      <span class="like-btn"></span>
      </div>

      <div class="image">
      <img src="item-2.png" alt=""/>
      </div>

      <div class="description">
      <span>Maison Margiela</span>
      <span>Future Sneakers</span>
      <span>White</span>
      </div>

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

      <div class="total-price" id="270"><input type="number" value="0" class="result-btn" /></div>
      </div>

      <!-- Product #3 -->
      <div class="item">
      <div class="buttons">
      <span class="delete-btn"></span>
      <span class="like-btn"></span>
      </div>

      <div class="image">
      <img src="item-3.png" alt="" />
      </div>

      <div class="description">
      <span>Our Legacy</span>
      <span>Brushed Scarf</span>
      <span>Brown</span>
      </div>

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

      <div class="total-price" id="349"><input type="number" value="0" class="result-btn" /></div>
      </div>
      </div>
      </body>


      Here the JS code:



      const items = document.querySelectorAll('.item');

      items.forEach(function(item) {
      const minusButton = item.querySelector('.minus-btn');
      const plusButton = item.querySelector('.plus-btn');
      const inputField = item.querySelector('.input-btn');
      const resultField = item.querySelector('.result-btn');

      minusButton.addEventListener('click', function minusProduct() {
      const currentValue = Number(inputField.value);
      if (currentValue > 0) {
      inputField.value = currentValue - 1;
      var x = item.querySelector('.total-price').id;
      resultField = item.querySelector('.result-btn').value = x* inputField.value;
      } else inputField.value = 0
      });

      plusButton.addEventListener('click', function plusProduct() {
      const currentValue = Number(inputField.value);
      if (currentValue < 100) {
      inputField.value = currentValue + 1;
      var x = item.querySelector('.total-price').id;
      resultField = item.querySelector('.result-btn').value = x* inputField.value;
      document.querySelector('.title').innerHTML = item.querySelector('.result-btn')[0].value
      +item.querySelector('.result-btn')[1].value + item.querySelector('.result-btn')[2].value
      } else inputField.value = 100
      });

      });






      javascript html5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 1:10









      Antonino

      1,53621327




      1,53621327










      asked Dec 30 '18 at 23:44









      hichem belfekihhichem belfekih

      81




      81
























          1 Answer
          1






          active

          oldest

          votes


















          0














          There are two thing that you need to do.
          1. You need to convert value of total price and total price to number.
          2. Set the innneHTML of dev other wise you will get the error. e.g. (



          Uncaught TypeError: Assignment to constant variable.
          at HTMLButtonElement.minusProduct
          )



          So, in your JS



          resultField.innerHTML = item.querySelector('.result-btn').value = Number(x) * Number(inputField.value);






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53982357%2fcalculate-the-sum-of-shopping-cart%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            There are two thing that you need to do.
            1. You need to convert value of total price and total price to number.
            2. Set the innneHTML of dev other wise you will get the error. e.g. (



            Uncaught TypeError: Assignment to constant variable.
            at HTMLButtonElement.minusProduct
            )



            So, in your JS



            resultField.innerHTML = item.querySelector('.result-btn').value = Number(x) * Number(inputField.value);






            share|improve this answer




























              0














              There are two thing that you need to do.
              1. You need to convert value of total price and total price to number.
              2. Set the innneHTML of dev other wise you will get the error. e.g. (



              Uncaught TypeError: Assignment to constant variable.
              at HTMLButtonElement.minusProduct
              )



              So, in your JS



              resultField.innerHTML = item.querySelector('.result-btn').value = Number(x) * Number(inputField.value);






              share|improve this answer


























                0












                0








                0







                There are two thing that you need to do.
                1. You need to convert value of total price and total price to number.
                2. Set the innneHTML of dev other wise you will get the error. e.g. (



                Uncaught TypeError: Assignment to constant variable.
                at HTMLButtonElement.minusProduct
                )



                So, in your JS



                resultField.innerHTML = item.querySelector('.result-btn').value = Number(x) * Number(inputField.value);






                share|improve this answer













                There are two thing that you need to do.
                1. You need to convert value of total price and total price to number.
                2. Set the innneHTML of dev other wise you will get the error. e.g. (



                Uncaught TypeError: Assignment to constant variable.
                at HTMLButtonElement.minusProduct
                )



                So, in your JS



                resultField.innerHTML = item.querySelector('.result-btn').value = Number(x) * Number(inputField.value);







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 0:04









                Narendra MongiyaNarendra Mongiya

                1,530719




                1,530719






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53982357%2fcalculate-the-sum-of-shopping-cart%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas