# Set multiple Button events with a for loop

**URL:** <https://forum.kirupa.com/t/set-multiple-button-events-with-a-for-loop/24264>\
**Category:** Uncategorized\
**Created:** [June 27, 2003, 10:17pm UTC](https://forum.kirupa.com/t/set-multiple-button-events-with-a-for-loop/24264 "2003-06-27T22:17:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VandenHanenberg](https://avatars.discourse-cdn.com/v4/letter/v/848f3c/32.png) [@VandenHanenberg](https://forum.kirupa.com/u/VandenHanenberg)\
**Post date:** [June 27, 2003, 10:17pm UTC](https://forum.kirupa.com/t/set-multiple-button-events-with-a-for-loop/24264/1 "2003-06-27T22:17:16Z")

</div>

Question:

I have the following piece of script. Works like charm, and why should it not.

\_root.card1.but.onRelease = function() {  
\_root.card1.gotoAndStop(2);  
if (\_root.firstcard==""){  
\_root.firstcard=1;  
} else if (\_root.secondcard==""){  
\_root.secondcard=1;  
}  
}

The thing is, i have to ad this script to 55 more buttons. (\_root.card2.but, \_root.card3.but, etc) Instead of making 56 of the scripts, I tried to use a for loop. This is the loop I used.

for (i=1; i\<=56; i++){  
\_root[“card”+i].but.onRelease = function() {  
\_root[“card”+i].gotoAndStop(2);  
if (\_root.firstcard==""){  
\_root.firstcard=i;  
} else if (\_root.secondcard==""){  
\_root.secondcard=i;  
}  
}  
}

But it does not seem to work. Can anybody lend me a hand?

Thanks!

---

<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:** [June 28, 2003, 12:13am UTC](https://forum.kirupa.com/t/set-multiple-button-events-with-a-for-loop/24264/2 "2003-06-28T00:13:57Z")

</div>

Please use the AS vbCode tags :sigh:

Try something like this…  
[AS]for (i=1; i\<=56; i++){  
\_root[“card”+i].but.i = i;  
\_root[“card”+i].but.onRelease = function() {  
this.gotoAndStop(2);  
if (\_root.firstcard == “”){  
\_root.firstcard = this.i;  
} else if (\_root.secondcard == “”){  
\_root.secondcard = this.i;  
}  
}  
}[/AS]

The variable “i” cannot be read in the onRelease, so I had to dynamically create a variable “i” attached to the \_root[“card”+i].but clip. I could then call the variable as “this.i”

Should work… in theory. 😛 (everyone knows how my theorys always end up)
