I'm setting up a package page with multiple packages/prices. To get an idea/example, similar to Spotify plans: https://www.spotify.com/nl/premium/#plans
The thing is that i want to update the price of the plan based on additional options, these options are checkboxes on the page. It can be none selected, one selected or multiple selected.
I managed to get so far to get it working for one checkboxes, but can't get it working for multiple checkboxes if i just copy the code. Here's my code:
Can someone help met to set it up for multiple checkboxes?
function calculate() { var j = document.getElementById("output"); let span = document.getElementById("lala"); var rege = /^[0-9]*$/; var tons = 12; if (rege.test(tons)) { val = parseInt(tons); var treesSaved = 10.45; if ($('input[name="mv-box"]').is(":checked")) { span.textContent = treesSaved + 4; } else { span.textContent = treesSaved; } } else alert("Error in input"); } $(function() { $('input[name="mv-box"]').change(function() { calculate(); }); }); <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <input type="checkbox" name="mv-box" value="1" aria-invalid="false"> MV-box + 4 euro <input type="checkbox" name="airco" value="1" aria-invalid="false"> Airco + 6 euro <br /> <span id="lala">10.45</span> euro
span.textContentdirectly, but calculate the price in a variable first. Assign the value of that variable, after you are done with going through all relevant checkboxes.data-amount="4") and then retrieve all checked boxes and adding theirdata-amountattributes.