I have a form that's a food menu, and I am having trouble with a section with check-boxes.
I wanted to make the form nicer so I styled the check-boxes as buttons so that when clicked the box gets checked. This works fine, but the issue is when I move from one check-box to another without unchecking the first one. It doesn't subtract the amount from my total.
I used the following jQuery script:
$('.drinks').on('change', function() {
$('.drinks').not(this).prop('checked', false);
});
to switch from one checkbox to the next automatically which shows up graphically but in my console it doesn't actually uncheck the other boxes.
$(document).ready(function(){
console.log("Jquery DOM ready.");
let totalprice="200";
$('#pricedisplay').html("$" + totalprice);
$('input[name=drink-one]').change(function(){
console.log("an input just changed.");
$('input[name=drink-one]:checked').each(function(){
let oneAddon = this.value;
console.log(this.value);
totalprice = parseFloat(totalprice) + parseFloat(oneAddon);
console.log(totalprice);
});
$('input[name=drink-one]:not(:checked)').each(function (){
let oneAddon = this.value;
console.log(this.value);
totalprice = parseFloat(totalprice) - parseFloat(oneAddon);
console.log(totalprice);
});
$('#pricedisplay').html("$" + totalprice)
});
$('input[name=drink-two]').change(function(){
console.log("an input just changed.");
$('input[name=drink-two]:checked').each(function(){
let twoAddon = this.value;
console.log(this.value);
totalprice = parseFloat(totalprice) + parseFloat(twoAddon);
console.log(totalprice);
});
$('input[name=drink-two]:not(:checked)').each(function (){
let twoAddon = this.value;
console.log(this.value);
totalprice = parseFloat(totalprice) - parseFloat(twoAddon);
console.log(totalprice);
});
$('#pricedisplay').html("$" + totalprice)
});
$('input[name=drink-three]').change(function(){
console.log("an input just changed.");
$('input[name=drink-three]:checked').each(function(){
let threeAddon = this.value;
console.log(this.value);
totalprice = parseFloat(totalprice) + parseFloat(threeAddon);
console.log(totalprice);
});
$('input[name=drink-three]:not(:checked)').each(function (){
let threeAddon = this.value;
console.log(this.value);
totalprice = parseFloat(totalprice) - parseFloat(threeAddon);
console.log(totalprice);
});
$('#pricedisplay').html("$" + totalprice)
});
$('.drinks').on('change', function() {
$('.drinks').not(this).prop('checked', false);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<body>
<div class="chks">
<div id="ck-button-one">
<label>
<input class="drinks" type="checkbox" name="drink-one" value="5"><span>One</span></input>
</label>
</div>
<div id="ck-button-two">
<label>
<input class="drinks" type="checkbox" name="drink-two" value="10"><span>Two</span></input>
</label>
</div>
<div id="ck-button-three">
<label>
<input class="drinks" type="checkbox" name="drink-three" value="20"><span>Three</span></input>
</label>
</div>
</div>
<div class="price">Purchase Price:
<span id="pricedisplay"></span>
</div>
</body>
</html>
When I click on the button, the total price adds correctly. If I click the same button, it subtracts correctly. If I click another button before un-clicking the previous button, the whole thing breaks and just keeps adding because you can't get back to the other button you clicked previously to undo its value.
I need help to make this function do the following:
if a user doesn't want any extra drinks, the price stays the same.
if a user chooses 'drink one', the price goes up by 5.
if a user clicks on 'drink one's' button again, the price does down by 5.
if a user clicks on 'drink one's' button, then immediately clicks on 'drink two's' button and the price will only add 'drink two's' value and subtract 'drink one's' value. The same goes for 'drink-three' and so on.
only one button can be checked at a time or no buttons can be checked at a time.
I tried to include console logs for the different steps to help in debugging.