# Is there a way to generalize an id for an onEvent?

**URL:** <https://forum.code.org/t/is-there-a-way-to-generalize-an-id-for-an-onevent/15664>\
**Category:** CS Principles\
**Tags:** applab\
**Created:** [April 18, 2018, 9:33pm UTC](https://forum.code.org/t/is-there-a-way-to-generalize-an-id-for-an-onevent/15664 "2018-04-18T21:33:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlarkin](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@dlarkin](https://forum.code.org/u/dlarkin)\
**Post date:** [April 18, 2018, 9:33pm UTC](https://forum.code.org/t/is-there-a-way-to-generalize-an-id-for-an-onevent/15664/1 "2018-04-18T21:33:43Z")

</div>

Is there a way to generalize an id for an onEvent?

Say I have 3 buttons, named button1, button2, and button3, and they are all doing something similar.  
Is there a way I could do something like indicate the button number somewhere else and then do an onEvent like:  
onEvent (“button”+num, …)

where var num contains a number 1, 2, or 3

The idea is to try and avoid having 30 onEvents instructions if they are all doing the pretty much the same thing.

Right now, I think the answer is no, but maybe someone else has an idea.

---

<div class="post-metadata">

**Author:** ![frank\_w\_lee](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/frank_w_lee/32/4895_2.png) [@frank\_w\_lee](https://forum.code.org/u/frank_w_lee)\
**Post date:** [April 18, 2018, 10:39pm UTC](https://forum.code.org/t/is-there-a-way-to-generalize-an-id-for-an-onevent/15664/2 "2018-04-18T22:39:22Z")

</div>

Hi @dlarkin,

Recently another teacher created an example of using a for-loop to generate multiple buttons with associated events: [Link to project](https://studio.code.org/projects/applab/WVw_r8mdpwB2dRK6hEs9Ng)

([Here’s the original forum post.](https://forum.code.org/t/functions-and-event-handlers/15418/4?u=frank_w_lee))

So I think the answer to your question is “Yes, that’s possible”. However, I’m not sure what are your other requirements of these buttons and events, but hopefully your needs can be filled using a similar for-loop.

Does that help?

Frank
