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

Pass a function when calling onClick for a button (UI)

Advocate ,
Jan 24, 2013 Jan 24, 2013

Hi

I´m now studying and adventuring myself in the Script UI creation. I´m enjoying it !!!

I´m having the following trouble: I added a button to a dialog box and would like to tell script to run some commands when users click on the button. So this is a simple (poor example):

var dlg = new Window ("dialog", "My dialog", [0,0,0,0])

dlg.size = [500,500]

dlg.location = [300,300]

var b = dlg.add("button", [0,0,0,0], "Run")

b.location = [20, 180]

b.size = [80, 30]

dlg.show()

b.onClick = test () //the problem is here

function test(){

  alert("teste")

}

-------

Everything is right. But the onClick event does not execute the function test I´m passing! What Am I missing?

I know if I use

b.onClick = function (){

...

}

It works...but I´d like the function to be called instead of directlly created when calling the onClick. Do not know if I´m able to explain it better.

Thank you for the help

Best Regards

Gustavo.

TOPICS
Actions and scripting
2.4K
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 1 Correct answer

Valorous Hero , Jan 24, 2013 Jan 24, 2013

The norm would be ...

var dlg = new Window ("dialog", "My dialog", [0,0,0,0])
dlg.size = [500,500]
dlg.location = [300,300]
var b = dlg.add("button", [0,0,0,0], "Run")
b.location = [20, 180]
b.size = [80, 30]
b.onClick = function(){ //the problem is here
test();
}
dlg.show()

function test(){
  alert("teste")
}

Translate
Adobe
Advocate ,
Jan 24, 2013 Jan 24, 2013

P.S: the way I wrote my example....function will execute when I close the dlg dialog.

And if I write the function at beggining it will autoexecute before dialog box be showed.

Looks it´s running the function automatically in the sequence of the commands! Not what I expect to be.

Thank you a lot

Gustavo.

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
Valorous Hero ,
Jan 24, 2013 Jan 24, 2013

The norm would be ...

var dlg = new Window ("dialog", "My dialog", [0,0,0,0])
dlg.size = [500,500]
dlg.location = [300,300]
var b = dlg.add("button", [0,0,0,0], "Run")
b.location = [20, 180]
b.size = [80, 30]
b.onClick = function(){ //the problem is here
test();
}
dlg.show()

function test(){
  alert("teste")
}

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
Advocate ,
Jan 24, 2013 Jan 24, 2013

Hi Paul

Interesting. So EVERY onClick, onClose, onOpen, onChange ... events must receive a function??

And sure we can call other functions inside them.

Thank you a lot

Gustavo.

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
Guru ,
Jan 24, 2013 Jan 24, 2013

Interesting. So EVERY onClick, onClose, onOpen, onChange ... events must receive a function??

No, you don't need to create a function for every control or evert type. Some controls have default handlers and you only need to define new ones if you want to change the default action.

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
Advocate ,
Jan 24, 2013 Jan 24, 2013
LATEST

Thank you Michael. That worked too.

Can you give an example of controls with default handler that I do not need to create a function?

Thank you a lot

Best Regards

Gustavo.

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
Guru ,
Jan 24, 2013 Jan 24, 2013

You need to define the control's callback before you show the dialog so you should move b.onClick = test; to above the dlg.show() line.

If you want the onClick to use the test function use b.onClick = test;

b.onClick = test() asssigns the results of the test function( if any ) to the onClick so here it's undefined.

I have found that sometimes using alert or $.writeln doesn't work in a dialog control event callback.

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