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

Script issue: Checkbox is returning false, even when checked

Contributor ,
Sep 12, 2024 Sep 12, 2024

Copy link to clipboard

Copied


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

Views

177

Translate

Translate

Report

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
...

Votes

Translate

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

Votes

Translate

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

Copy link to clipboard

Copied

You need to check checkbox.value

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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