# Help with function created in for loop

**URL:** https://forum.kirupa.com/t/help-with-function-created-in-for-loop/253486
**Category:** flash
**Created:** [March 1, 2008, 10:30pm UTC](https://forum.kirupa.com/t/help-with-function-created-in-for-loop/253486 "2008-03-01T22:30:45Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![bhadaway](https://avatars.discourse-cdn.com/v4/letter/b/c2a13f/32.png) [@bhadaway](https://forum.kirupa.com/u/bhadaway)
#### Post date: [March 1, 2008, 10:30pm UTC](https://forum.kirupa.com/t/help-with-function-created-in-for-loop/253486/1 "2008-03-01T22:30:45Z")

</div>

I have the following code that should set the onPress events for movieclips

```auto
function actionSetter() {        
        
        for(var k = 0; k< 5; k++) {
            
            trace(k); //traces correctly

            _level0["EventBtn" + k].onPress = function() {
             
                trace(k); //always traces as 5 regardless of which btn is pressed
            }
            
        }
    }

```

What should I do to correctly define onPress for my movieclips within the loop?

Help please
