I've been trying to figure out how, but struggling.
I've got a page with a series of checkboxes, Radio buttons and every time a client checks one I'd like to add a set amount to a total. When they uncheck it I'd like the total to no longer include that amount. The total needs to be displayed on the same page as the checkboxes. Anyone have any idea what I need to code to make it happen?
I am using the following code but unable to figure it out how to use the code as i am new and trying complete y project
function calculateTotal() { let total = 0; if ($w('#checkbox1').checked) {total += Number($w('#checkbox1').value)} (e.g value is - 20) if ($w('#checkbox2').checked) {total += Number($w('#checkbox2').value)} (e.g value is - 20) if ($w('#checkbox3').checked) {total += Number($w('#checkbox3').value)} (e.g value is - 20) if ($w('#checkbox4').checked) {total += Number($w('#checkbox4').value)} (e.g value is - 20) $w('#total').text = String(total); (total should be 80 here) }
Thanks
Rajat
Since you are using CheckBox-Groups and not normal CheckBoxes, you will have to work with CheckBox-Groups in a little bit different way.
function manageSum() { var sum = 0; if ($w("#checkboxGroup1").checked) { sum += Number($w("#checkboxGroup1").value); console.log("Sum0: = ", Number($w("#checkboxGroup1").value) console.log("Sum1: = ", Number($w("#checkboxGroup1").value[0]) console.log("Sum1: = ", Number($w("#checkboxGroup1").value[1]) console.log("Sum2: = ", sum) } else{ } $w("#text141").text = "" + sum; }
What are the results in the CONSOLE?
Show a screenshot of console.log
$w.onReady(function () { $w("#checkboxGroup1").value = [1]===true; $w("#checkboxGroup2").value = "2"; $w("#checkboxGroup1").onChange((event, $w) => { manageSum(); }); $w("#checkboxGroup2").onChange((event, $w) => { manageSum(); }); manageSum(); }); function manageSum() { var sum = 0; if ($w("#checkboxGroup1").checked) sum += Number($w("#checkboxGroup1").value); if ($w("#checkboxGroup2").checked) sum += Number($w("#checkboxGroup2").value); $w("#text141").text = "" + sum; }
This is the code
😣 code without code-tags 😁