Skip to main content
Inspiring
September 13, 2024
Answered

Script issue: Checkbox is returning false, even when checked

  • September 13, 2024
  • 3 replies
  • 596 views


Hi all,

Could anyone please help me to figure out how to fix the below script? Checkbox is returning false, even when checked.


var myWindow = new Window("dialog");
myWindow.orientation = "column";

var test = '';
var button = myWindow.add("button", undefined, "Run");
var checkbox = myWindow.add("checkbox", undefined, "Checkbox");
button.onClick = function() {test = '0'; myWindow.close()};
myWindow.show();

if (test != '') main(test);

function main(){

if (checkbox == true)
    {
    alert("Checkbox returned true");
}else{
    alert("Checkbox returned false");
}
}
Correct answer FRIdNGE

It seems enough:

 

var myWindow = new Window("dialog");

    var button = myWindow.add("button", undefined, "Run");
    var checkbox = myWindow.add("checkbox", undefined, "Checkbox");
    button.onClick = function() { myWindow.close() };
    myWindow.show();

if ( checkbox.value == true ) alert("Checkbox returned true")
else alert("Checkbox returned false")

 

(^/)  The Jedi

3 replies

FRIdNGE
FRIdNGECorrect answer
September 13, 2024

It seems enough:

 

var myWindow = new Window("dialog");

    var button = myWindow.add("button", undefined, "Run");
    var checkbox = myWindow.add("checkbox", undefined, "Checkbox");
    button.onClick = function() { myWindow.close() };
    myWindow.show();

if ( checkbox.value == true ) alert("Checkbox returned true")
else alert("Checkbox returned false")

 

(^/)  The Jedi

m1b
Community Expert
Community Expert
September 13, 2024

I recommend you keep the checkbox value in a variable that you declare outside the ScriptUI Window scope. Then you make an event listener "onClick" that updates it whenever user clicks the checkbox.

 

Here's one way to code it:

var myCheckBoxValue = false;

var myWindow = new Window("dialog");
myWindow.orientation = "column";

var test = '';
var button = myWindow.add("button", undefined, "Run");
var checkbox = myWindow.add("checkbox", undefined, "Checkbox");

checkbox.onClick = function () { myCheckBoxValue = this.value };
button.onClick = function () { test = '0'; myWindow.close() };
myWindow.show();

if (test != '') main(test);

function main() {

    if (myCheckBoxValue == true) {
        alert("Checkbox returned true");
    } else {
        alert("Checkbox returned false");
    }
}

 - Mark

brian_p_dts
Community Expert
Community Expert
September 13, 2024

You need to check checkbox.value