Skip to main content
dublove
Legend
July 4, 2025
Answered

How do I get the latest input when I click on it? Instead of the default value?

  • July 4, 2025
  • 1 reply
  • 883 views

The default value in the input box is: “Lu Jon”.
But after I re-enter "Mark" and click OK,
I find that the value I get from "yourName.text" is not updated, it is still "Lu Jon".

What's wrong?

 

My friends, help me look, please~

Thank you very much.

 

 

// Create a new window
var win = new Window("dialog", "Nmae");
var inputGroup = win.add("group");
inputGroup.add("statictext", undefined, "inPut Name");

var yourName = inputGroup.add("edittext", undefined, "Lu Jon");
yourName.characters = 20;
inputText = yourName.text;

// Buttons group
var btnGroup = win.add("group");
btnGroup.alignment = "right";

var okBtn = btnGroup.add("button", undefined, "OK");   // OK
var cancelBtn = btnGroup.add("button", undefined, "Cancel"); // Cancel

// Button click events
okBtn.onClick = function () {
    alert(inputText);
    //rwJSON();
    win.close();
};

cancelBtn.onClick = function () {
    win.close();
};

// Show the dialog
win.center();
win.show();

 

Correct answer m1b

Hi @m1b 

In this case, you first need to enter the value of aa on the left side.
When you tap the convert button, a function is executed.
Then the result is displayed on the right.

The variables are cross-referenced and I don't know how to get it to work.

var aa, bb, cc;
function aToc() {
    //Suppose here is an execution that ends up with cc
    bb = aa + "dfdf";
    cc = bb + 999
}
(function () {
    var yourName;
    // Create a new window
    var win = new Window("dialog", "Nmae");
    var inputGroup = win.add("group");
    inputGroup.add("statictext", undefined, "Input aa");

    var aaNameField = inputGroup.add("edittext", undefined, "Please enter the value of aa");
    aaNameField.characters = 20;
    inputGroup.add("statictext", undefined, "To");


    var yourNameField = inputGroup.add("edittext", undefined, cc);
    yourNameField.characters = 20;

    // Buttons group
    var btnGroup = win.add("group");
    btnGroup.alignment = "right";

    var okBtn = btnGroup.add("button", undefined, "Converts");   // OK
    var cancelBtn = btnGroup.add("button", undefined, "Cancel"); // Cancel

    // Button click events
    okBtn.onClick = function () {
        yourName = yourNameField.text; // Get the input value
        //rwJSON();
        win.close(1);
    };

    cancelBtn.onClick = function () {
        win.close(2);
    };

    // Show the dialog
    win.center();
    win.show();
    alert(yourName ? "Your name is: " + yourName : "You didn't enter a name.");
})();

 


@dublove it can be like this:

(function () {

    var yourName;
    // Create a new window
    var win = new Window("dialog", "Nmae");
    var inputGroup = win.add("group");
    inputGroup.add("statictext", undefined, "Input aa");

    var aaNameField = inputGroup.add("edittext", undefined, "Please enter the value of aa");
    aaNameField.characters = 20;
    
    var bbText = win.add("statictext", undefined, 'BB will be shown here.');

    // Buttons group
    var btnGroup = win.add("group");
    btnGroup.alignment = "right";

    var okBtn = btnGroup.add("button", undefined, "Converts");   // OK
    var cancelBtn = btnGroup.add("button", undefined, "Cancel"); // Cancel

    // Button click events
    okBtn.onClick = function () {
        yourName = aToc(aaNameField.text); // Get the input value
        //rwJSON();
        win.close(1);
    };

    cancelBtn.onClick = function () {
        win.close(2);
    };

    aaNameField.onChanging = function () {
        bbText.text = aToc(aaNameField.text);
    };

    // Show the dialog
    win.center();
    win.show();
    alert(yourName ? "Your name is: " + yourName : "You didn't enter a name.");
})();

function aToc(str) {
    //Suppose here is an execution that ends up with cc
    var bb = str + "dfdf";
    return bb;
};

 

or can be like this:

(function () {

    var aa;
    var yourName;

    // Create a new window
    var win = new Window("dialog", "Nmae");
    var inputGroup = win.add("group");
    inputGroup.add("statictext", undefined, "Input aa");

    var aaNameField = inputGroup.add("edittext", undefined, "Please enter the value of aa");
    aaNameField.characters = 20;
    
    inputGroup.add("statictext", undefined, "To");
    var yourNameField = inputGroup.add("edittext", undefined, aa);
    yourNameField.characters = 20;

    // Buttons group
    var btnGroup = win.add("group");
    btnGroup.alignment = "right";

    var okBtn = btnGroup.add("button", undefined, "Converts");   // OK
    var cancelBtn = btnGroup.add("button", undefined, "Cancel"); // Cancel

    // Button click events
    okBtn.onClick = function () {
        yourName = aToc(aaNameField.text);
        yourNameField.text = yourName;
        //rwJSON();
        // win.close(1);
    };

    cancelBtn.onClick = function () {
        win.close(2);
    };

    // Show the dialog
    win.center();
    win.show();
    alert(yourName ? "Your name is: " + yourName : "You didn't enter a name.");
})();

function aToc(str) {
    //Suppose here is an execution that ends up with cc
    var bb = str + "dfdf";
    return bb;
};

1 reply

m1b
Community Expert
Community Expert
July 4, 2025

@dublove there are many ways, but perhaps something like this...

 

(function () {

    var yourName;

    // Create a new window
    var win = new Window("dialog", "Nmae");
    var inputGroup = win.add("group");
    inputGroup.add("statictext", undefined, "Input Name");

    var yourNameField = inputGroup.add("edittext", undefined, "Lu Jon");
    yourNameField.characters = 20;

    // Buttons group
    var btnGroup = win.add("group");
    btnGroup.alignment = "right";

    var okBtn = btnGroup.add("button", undefined, "OK");   // OK
    var cancelBtn = btnGroup.add("button", undefined, "Cancel"); // Cancel

    // Button click events
    okBtn.onClick = function () {
        yourName = yourNameField.text; // Get the input value
        //rwJSON();
        win.close(1);
    };

    cancelBtn.onClick = function () {
        win.close(2);
    };

    // Show the dialog
    win.center();
    win.show();

    alert(yourName ? "Your name is: " + yourName : "You didn't enter a name.");

})();

- Mark

dublove
dubloveAuthor
Legend
July 4, 2025

Hi  m1b.

It works just fine.

extremely grateful.

 

yourName = yourNameField.text;

 

Just different here?

The next line is also a bit magical:

 

alert(yourName ? "Your name is: " + yourName : "You didn't enter a name.");

 

 

m1b
Community Expert
Community Expert
July 4, 2025

First I declared a variable "yourName" to store the string.

var yourName;

 

Then I assigned it the value of the text of the edit field.

yourName = yourNameField.text;

 

But, importantly, I assigned the value in the okayButton's onClick event handler. There are other places you could assign it (eg. in the edittext's "onChanged" event handler), but that is a good place to do it.

 

Also I named the variables better by calling them what they actually are, eg. inputGroup and yourNameField.

- Mark

 

P.S. the last line is not important to your question. It uses the ternary operator.