# Need help with buttons and array

**URL:** <https://discourse.processing.org/t/need-help-with-buttons-and-array/17117>\
**Category:** Coding Questions\
**Created:** [January 16, 2020, 10:16am UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117 "2020-01-16T10:16:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TjarkTV](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tjarktv/32/7822_2.png) [@TjarkTV](https://discourse.processing.org/u/TjarkTV)\
**Post date:** [January 16, 2020, 10:16am UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117/1 "2020-01-16T10:16:08Z")

</div>

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. ☹  
Here is my code without that function in the webeditor: [https://editor.p5js.org/TjarkTV/sketches/h\_dgXDTqj](https://editor.p5js.org/TjarkTV/sketches/h_dgXDTqj)

Please help me. 😉

---

<div class="post-metadata">

**Author:** ![manno](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@manno](https://discourse.processing.org/u/manno)\
**Post date:** [January 16, 2020, 1:33pm UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117/2 "2020-01-16T13:33:04Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![TjarkTV](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tjarktv/32/7822_2.png) [@TjarkTV](https://discourse.processing.org/u/TjarkTV)\
**Post date:** [January 16, 2020, 1:37pm UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117/3 "2020-01-16T13:37:30Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![manno](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@manno](https://discourse.processing.org/u/manno)\
**Post date:** [January 16, 2020, 5:59pm UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117/4 "2020-01-16T17:59:19Z")

</div>

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

[https://editor.p5js.org/manno/sketches/cVt3I\_8aw](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.

---

<div class="post-metadata">

**Author:** ![TjarkTV](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tjarktv/32/7822_2.png) [@TjarkTV](https://discourse.processing.org/u/TjarkTV)\
**Post date:** [February 5, 2020, 2:39pm UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117/5 "2020-02-05T14:39:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![manno](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@manno](https://discourse.processing.org/u/manno)\
**Post date:** [February 26, 2020, 8:14pm UTC](https://discourse.processing.org/t/need-help-with-buttons-and-array/17117/6 "2020-02-26T20:14:22Z")

</div>

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