# Assign variables do dynamic created buttons?

**URL:** <https://forum.kirupa.com/t/assign-variables-do-dynamic-created-buttons/321173>\
**Category:** flash\
**Created:** [April 12, 2011, 2:49pm UTC](https://forum.kirupa.com/t/assign-variables-do-dynamic-created-buttons/321173 "2011-04-12T14:49:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugot](https://avatars.discourse-cdn.com/v4/letter/h/45deac/32.png) [@hugot](https://forum.kirupa.com/u/hugot)\
**Post date:** [April 12, 2011, 2:49pm UTC](https://forum.kirupa.com/t/assign-variables-do-dynamic-created-buttons/321173/1 "2011-04-12T14:49:44Z")

</div>

Hi:

I have a 13 buttons to wich I added Event listeners using a for loop:

```auto
for (var i:Number = 1; i < 13; i++)
{
        this["main_bt_" + i].addEventListener(MouseEvent.CLICK, onClick);
}

function onClick(e:MouseEvent):void
{
    trace(i);
}

```

When clicking the button it should return the variable i that generated it (1,2,3…etc)

It always returns the last i (13).

How can I get this to work? Do I have to create the buttons manually and add a functions to each button?

Thanks!
