Question about Checkboxes, Radio buttons and Sums in total

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

:persevere: code without code-tags :grin:

$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

Much better! I will take alook.

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

Since I am a begginer so do have much knowledge about coding still i am trying to

Please have a look of the consol


I want result in Text Textbox

Hi Dear I have tried changing tital from checkbox-group to checkbox only now code is working fine

I dint even noticed my mistake thank you so so much mate you have save my alot of time :blush:

@rjtsuri1000
No problem! Remember —> CONSOLE = YOUR BEST-FRIEND !
Work more with CONSOLE and you will be able to solve your own problems in many cases!:wink: