how to react to clicks on a dynamically created button with JS

Hanna's icon

Hi!

I could use some help with the following situation:
I use javascript to dynamically create my interface, which mainly consists of standart Max objects such as buttons and [live.text] objects. The interface looks as intended and the buttons work as expected. However, now I would like to react to the clicks on those buttons with a javascript function. I tried by defining (or overwriting?) the onclick-function for those buttons but this does not seem to work.

What I tried/what I am looking for is something in the lines of:
myButton = this.patcher.newdefault(buttonXPosition, buttonYPosition, "live.text", "aName");
myButton.onclick = someFunction();

I found some workarounds (e.g., connecting all buttons to their own individual message boxes and sending those messages back to my javascript code), but all of them slow down my patch immensely. I would like to know whether this is possible and if yes, how....

Thank you so much for your help! I appreciate it.
Best,
Hanna

hardcoder's icon

I searched the attributes of live.text and button but there does not seem to be an attribute like "clicked". I would do it like you describe it in your workaround.

Concerning the slow down of the patch: did you think about creating these message IDs before runtime?

Hanna's icon

Thanks for your quick reply! Yes, I am creating the message IDs before runtime. Unfortunately, my interface is dynamic/changing according to the choices of the user during the performance... so there will be new buttons (and hence messages) created almost constantly... I will explore this further :D Thanks again!

Lee's icon

as far as I am aware, this is not possible. you need to take an outlet from the button to an inlet in the javascript and process it there - you can dynamically do this when you create the button.

if anyone knows of any other way, I'd also like to hear...

Hanna's icon

thanks - I'll let you know in case I find another way.... ! cheers :)

Hanna's icon

Just wanted to let you know that I made my own jsui buttons to gain access to the on click function. Cheers, Hanna

Lee's icon

Nice idea. Do you have an example?

Hanna's icon

An example? Well, I used the example buttons in the jsui-helpfile as a starting point/example. I modified one of the example buttons a bit and called the jsui button "myjsbutton.js". Then I can dynamically create the jsui-buttons like this with JS (makemyinterface.js):

myButtons[count] = this.patcher.parentpatcher.newdefault(tagXPosition, tagYPosition, "jsui", "tag");
myButtons[count].filename("myjsbutton.js");
myButtons[count].jsarguments(tagName);
myButtons[count].varname = "tag"+count;

In the myjsbutton.js file, I used this code to report the clicks to a [r receiver] object (make sure to give it the scripting name "receiver").

function onclick()
{
    messnamed("receiver", mytexton);

}

I connect the [r receiver] object to the makemyinterface.js file.

I am not sure if this is the best way to do this, but it is at least one way to do it. The only disadvantage I found is that one can not scroll the patch when the mouse is over a jsui object.....