Need help with buttons and array

hey guys I have a problem. I want to make a programm where i can press buttons to add something to an array which i can read, so like an numpad. the layout and all is great but when i want to add the function that when i press a button the number is add to the array nothing happens. :frowning:
Here is my code without that function in the webeditor: https://editor.p5js.org/TjarkTV/sketches/h_dgXDTqj

Please help me. :wink:

1 Like

Would this do what you want (add it right above k++)?

let index = k;
buttons[k].button.mousePressed(()=>{
    input.push(index);
});
1 Like

When I copy-paste it there this massage comes up in the console:

TypeError: buttons[k].button is undefined (sketch: line 18)

Sorry, my bad. I changes something in the class as well: a getter to the button.

https://editor.p5js.org/manno/sketches/cVt3I_8aw

The class now returns a reference to the HTML button by means of the getter. that is used to attached the mousePressed to the button.

2 Likes

heyy i just wanted to thank you because it worked and I forgot to Thank you. :hugs:

3 Likes

You’re welcome :grinning: (was away for a bit…)