<!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);
});
})