Switch code syntax

Could someone please help with how to correctly write the code for this? When I try using the ‘get checked’ or ‘set checked’ command I see an error " ‘get’/‘set’ is undefined. If I put $w in front I get an ‘unexpected token’ error. Also, what are the default values for a switch and why does the settings panel for a switch only have one value in it rather than one for on, one for off? I’ve read the code API but am unsure how to use this in an actual code line:

get checked(): boolean
set checked(value: boolean): void

Does anyone have any code examples for this element?

1 Like

You can set and get the checked variable like any other variable. for example:

let bool = $w(#switch1).checked; // get the checked value of "switch1"
$w(#switch2).checked = bool; // set checked for switch2

Good luck :slight_smile:

1 Like

Thanks for that. That makes sense but the info above that I copied from the Wix reference doesn’t!

What about to Set Switch to uncheck(off) at button click ?

What is the syntax to set uncheck ?

This unchecks a switch:
$w(#switch).checked = false;

This checks a switch:
$w(#switch).checked = true;

1 Like

Thanks! I code in that way : (just for user infos)

let value = $w("#switch2").checked ;
let myCheckbox2 = await $w("#switch2");
myCheckbox2.checked = await !myCheckbox2.checked;
await $w("#dataset2").setFieldValue("status",false);