I cannot get my form to add up the values of the components of a necklace to display a final price on my website.
This is my code:
<div class="c-query">
<div class="c-title">
<h2 class="js-split">
Select diamond size</h2>
</div>
<div class="c-reply">
<div style="--order:1">
<input type="radio" name="diamondsize" id="1ct" value="450">
<label style="display:flex; align-items:center; justify-content:center;" for="design">
1 ct <span style="font-size:30px;">○</span></label>
</div>
<div style="--order:2">
<input type="radio" name="diamondsize" id="2ct" value="1000">
<label style="display:flex; align-items:center; justify-content:center;"for="des&dev">
2 ct <span style="font-size:40px;">○</span></label>
</div>
<div style="--order:3">
<input type="radio" name="diamondsize" id="3ct" value="1500">
<label style="display:flex; align-items:center; justify-content:center;"for="mob-des">
3 ct <span style="font-size:50px;">○</span></label>
</div>
</div>
</div>
<div class="c-query">
<div class="c-title">
<h2 class="js-split">
Select length</h2>
</div>
<div class="c-reply">
<div style="--order:1">
<input type="radio" name="chainlength" id="16in" value="16in">
<label for="range1">
16 inch: neckline</label>
</div>
<div style="--order:2">
<input type="radio" name="chainlength" id="18in" value="18in">
<label for="range2">
18 inch: collarbone</label>
</div>
<div style="--order:3">
<input type="radio" name="chainlength" id="20in" value="20in">
<label for="range3">
20 inch: collar to bust</label>
</div>
<div style="--order:4">
<input type="radio" name="chainlength" id="22in" value="22in">
<label for="range4">
22 inch: bust</label>
</div>
</div>
</div>
<div class="c-query">
<div class="c-title">
<h2 class="js-split">
Select metal</h2>
</div>
<div class="c-reply">
<div style="--order:1">
<input type="radio" name="Source" id="ag925" value="299">
<label for="awww">
Silver 925</label>
</div>
<div style="--order:2">
<input type="radio" name="Source" id="au18" value="2999">
<label for="insp">
Gold 18K</label>
</div>
<div style="--order:3">
<input type="radio" name="Source" id="pl950" value="1999">
<label for="behance">
Platinum 950</label>
</div>
<div style="--order:4">
<input type="radio" name="Source" id="pd950" value="2999">
<label for="other">
Palladium 950</label>
</div>
</div>
</div>
<script>
let diamondSizeValue = 0;
let metalValue = 0;
document.querySelectorAll('input[name="diamondsize"]').forEach(input => {
input.addEventListener('change', function() {
diamondSizeValue = this.value;
console.log('Diamond size value:', diamondSizeValue);
updateTotalPrice();
});
});
document.querySelectorAll('input[name="Source"]').forEach(input => {
input.addEventListener('change', function() {
metalValue = this.value;
console.log('Metal value:', metalValue);
updateTotalPrice();
});
});
function updateTotalPrice() {
const totalPrice = parseInt(diamondSizeValue) + parseInt(metalValue);
document.querySelector('#total_price').innerHTML = `Total price: $${totalPrice}`;
console.log('Total price:', totalPrice);
}
</script>
<h1 class="js-split">
Price with shipping to <p id="country-name">your home</p></h1>
<p class="js-split">
<p id="currency-sym"></p> <h2 id="total_price"></h2>
I expected this code to display a total price adding up the two values when the radio buttons are selected, but instead it reads: "$ " and I'm not sure how to fix it. All help is appreciated.
tag and I still see "$ "
– gsam34565840 Mar 08 '23 at 10:27