Forums > Javascript

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

January 28, 2014 | 1:10 am

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


January 28, 2014 | 7:55 am

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?


January 28, 2014 | 8:06 am

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
January 28, 2014 | 2:24 pm

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…


January 30, 2014 | 2:24 am

thanks – I’ll let you know in case I find another way…. ! cheers :)


February 2, 2014 | 5:47 am

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



Lee
February 3, 2014 | 1:58 am

Nice idea. Do you have an example?


February 16, 2014 | 11:53 am

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


Viewing 8 posts - 1 through 8 (of 8 total)