Skip to main content
Known Participant
September 1, 2020
Question

Button onClick, not functioning

  • September 1, 2020
  • 2 replies
  • 825 views

Greetings.

 

I have a button defined as such

 

var execBtn = appWindow.add('button',undefined,'Export');

 

I then click on it thusly:

 

execBtn.onClick = function(){

someFunction();

 

}

 

The code in someFunction() runs fine outside of the button but will not execute when wrapped within the button. I triple checked that variables passed to someFunction were either global or defined within the scope of the button click itself. Not sure why it will not run. Is there some other way to do this like onButtonDown or something?

 

I appreciate the community's time and any assistance you can provide.

 

This topic has been closed for replies.

2 replies

Community Expert
September 2, 2020

Try using addEventListener method instead of onClick. Something like the following

execBtn.addEventListener("click", function(){

       someFunction()

})

-Manan

 

-Manan
Known Participant
September 2, 2020

This is also a great idea. Thank you!

Legend
September 1, 2020

Use try catch.

For tests use this

execBtn.onClick = function()
   {
    try {
        alert(1);
        someFunction();
        alert(2);
        }
    catch(e) { alert(e); }
    }

 

Known Participant
September 1, 2020

Thank you very much! I was wondering how I could do a "console.log()" type debug to catch where it was going wrong. Thanks again!

Legend
September 2, 2020

So the try script yielded no results, however apparently all I had to do was move the onClick call to right below where I defined the button because now it works. That makes no sense. With a globally defined variable I should be able to call it anywhere especially since the onClick call is ALSO taking place in the global scope. Anyone know why the built in listener is expecting the button call right after you define the button?

What clued me in was the fact that I had a close button which was working perfectly. I noticed that the call for the button click for the close button what right after the defining of the close button.


It's hard for me to understand what you write.
If you would give a more complete code that does not work for you, then it will be possible to explain the reason for your failure.

P.S. If you use addEventListener ("click"), your button will only respond to the mouse.