Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script issue: Checkbox is returning false, even when checked

Contributor ,
Sep 12, 2024 Sep 12, 2024


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");
}
}
TOPICS
Scripting
501
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Sep 12, 2024 Sep 12, 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 () { myC
...
Translate
Guide , Sep 13, 2024 Sep 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

Translate
Community Expert ,
Sep 12, 2024 Sep 12, 2024

You need to check checkbox.value

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2024 Sep 12, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 13, 2024 Sep 13, 2024
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines