# Attaching dynamic code to a button

**URL:** <https://forum.kirupa.com/t/attaching-dynamic-code-to-a-button/294150>\
**Category:** Uncategorized\
**Created:** [August 10, 2009, 12:13am UTC](https://forum.kirupa.com/t/attaching-dynamic-code-to-a-button/294150 "2009-08-10T00:13:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricmetal](https://avatars.discourse-cdn.com/v4/letter/r/b782af/32.png) [@ricmetal](https://forum.kirupa.com/u/ricmetal)\
**Post date:** [August 10, 2009, 12:13am UTC](https://forum.kirupa.com/t/attaching-dynamic-code-to-a-button/294150/1 "2009-08-10T00:13:34Z")

</div>

looking for help

i need to add code to attached buttons

**for(i=1; i\<=6; i++) {  
\_root.attachMovie(“niceButton”, “niceButton”+i, this.getNextHighestDepth(), {\_x: 50 \* i, \_y: 50});  
\_root[“niceButton”+i].onPress = function() {  
trace(i);  
}  
}**

this traces “7” for every button’s onPress!  
what i want is for each buttons to trace from 1 to 6, according to each attach.  
i’ve tried multiple version of the code but i either get undefined or “7”  
the result i’m after is the following code to be attached to each button:

**\_root.niceButton1.onPress = function() {  
trace(“1”);  
}**

i hope someone knows and can enlighten me on this please  
thanks
