# Assigning event to a bunch of buttons as once

**URL:** <https://forum.kirupa.com/t/assigning-event-to-a-bunch-of-buttons-as-once/59949>\
**Category:** Uncategorized\
**Created:** [August 5, 2004, 2:24am UTC](https://forum.kirupa.com/t/assigning-event-to-a-bunch-of-buttons-as-once/59949 "2004-08-05T02:24:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iloveitaly](https://avatars.discourse-cdn.com/v4/letter/i/7ea924/32.png) [@iloveitaly](https://forum.kirupa.com/u/iloveitaly)\
**Post date:** [August 5, 2004, 2:24am UTC](https://forum.kirupa.com/t/assigning-event-to-a-bunch-of-buttons-as-once/59949/1 "2004-08-05T02:24:32Z")

</div>

I am trying to assign events to a bunch of buttons at once. To test out to see if my theory works i tried this code with 3 buttons on the stage named test1, test2, ect.

```auto

var e=0
while(e!=4){
	_level0["test"+e].onRelease = function() {
		trace("tester"+e+"<<<")
	};
	e++
}

```

they each trace something, but its the same thing?! tester4\<\<\<  
Any ideas?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 5, 2004, 2:46am UTC](https://forum.kirupa.com/t/assigning-event-to-a-bunch-of-buttons-as-once/59949/2 "2004-08-05T02:46:52Z")

</div>

That happens because after the loop evaluates, the variable _i_ will be equal 4, therefore clicking any button will return 4.  
I think you want something like this:

```auto
while (e != 4) {
	_level0["test"+e].onRelease = function() {
		trace("tester"+this._name.substr(-1)+"<<<");
	};
	e++;
}

```

or

```auto
while (e != 4) {
	_level0["test"+e].f = e;
	_level0["test"+e].onRelease = function() {
		trace("tester"+this.f+"<<<");
	};
	e++;
}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 5, 2004, 12:48pm UTC](https://forum.kirupa.com/t/assigning-event-to-a-bunch-of-buttons-as-once/59949/3 "2004-08-05T12:48:20Z")

</div>

thanks excatly what i was looking for.
