0
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="./script.js"></script>
</head>
<body>
    <span id="totalPrice">Value</span>
    <span id="productId1">1000</span>
    <span id="productId2">2222</span>
    <span id="productId3">2222</span>
    <input type="checkbox" value="1"/>
    <input type="checkbox" value="2"/>
    <input type="checkbox" value="3"/>
    <input type="button" id="addButton" value="Add"/>
</body>
</html>

If I click on the checkbox, then I get its value and concatenate. As a result, I get the id for the span from which I want to take the value and sum it up. After that, display the entire amount in span

this is my js code:

$(function(){
  
  var totalPrice = $('#totalPrice');
  var boxes = $('input[type=checkbox]')
  var totalPriceNow = 0;

    boxes.click(function() {
      totalPriceNow = 0;
      boxes.each(function() {
        if (this.checked) {
          var productId = 'productId';
          productId += this.value;
          var productPrice = $('#' + 'productId').text();
          totalPriceNow += parseInt(productPrice);
        }
      });

      totalPrice.text(totalPriceNow);
  });
})
4
  • 1
    Any code attempt for javascript? Commented Jan 22, 2021 at 0:17
  • SO is not a coding service... Contributors expect an attempt from you about which it is possible to give explanations why your attempt failed. Commented Jan 22, 2021 at 0:19
  • And I found two duplicates for your question: this and that Commented Jan 22, 2021 at 0:28
  • I added everything I have) Don't swear) Commented Jan 22, 2021 at 0:39

2 Answers 2

0

Whenever any checkbox is change you can loop through all checkboxes which is checked and add your sum to some variable and finally add this value to your totalPrice.

Demo Code :

$('input[type=checkbox]').change(function() {
  var totalPriceNow = 0;
  //loop through checked checkboxes
  $('input[type=checkbox]:checked').each(function() {
    var productId = 'productId' + this.value;
    totalPriceNow += parseInt($('#' + productId).text().trim()); //get text
  })
  $('#totalPrice').text(totalPriceNow);
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

<span id="totalPrice">Value</span>
<span id="productId1">1000</span>
<span id="productId2">2222</span>
<span id="productId3">2222</span>
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
<input type="checkbox" value="3" />
<input type="button" id="addButton" value="Add" />

Sign up to request clarification or add additional context in comments.

Comments

0

// listen for change event on any "selected" checkbox
  $(document).on('change', '[name="selected"]', function () {
    // get selected checkbox values as array of numbers
    var selectedValues = $('[name="selected"]:checked').map(function () {
      // convert value from string to number
      return Number($(this).val());
    }).get();
    
    // add prices together to get total price
    var totalPrice = selectedValues.reduce(function (total, price) {
      return total + price;
    }, 0);
    
    $('#totalPrice').html(totalPrice);
  })
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<body>
    <span id="totalPrice">0</span>
    <input type="checkbox" name="selected" value="1">
    <input type="checkbox" name="selected" value="2">
    <input type="checkbox" name="selected" value="3">
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.