# For Loop - simple question

**URL:** <https://forum.kirupa.com/t/for-loop-simple-question/262316>\
**Category:** flash\
**Created:** [June 5, 2008, 12:36am UTC](https://forum.kirupa.com/t/for-loop-simple-question/262316 "2008-06-05T00:36:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soig](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Soig](https://forum.kirupa.com/u/Soig)\
**Post date:** [June 5, 2008, 12:36am UTC](https://forum.kirupa.com/t/for-loop-simple-question/262316/1 "2008-06-05T00:36:25Z")

</div>

Sorry to bug you with such an easy one but its been two years for me since I’ve been away from Actionscript!

I’m using a For loop to program the buttons on my website. The first time I use _i_ to associate the thumbNail functions it works well. However when I actually do click the button, the second _i_ always refers to 12 - the final number in that loop.

How do I get the second \*i \*to correspond to the first _i_? I know its an easy fix, but I’m stumped!!! Thanks

```auto

for (i = 0; i < 12; i++) {
    mcBody.mainBody["thumbNail"+ i].onRelease = function() {
        _root.mcBody.mainBody.textCaption.textDisp = thumbNailCaption ***** ;
        }
}

```

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [June 5, 2008, 12:50am UTC](https://forum.kirupa.com/t/for-loop-simple-question/262316/2 "2008-06-05T00:50:38Z")

</div>

Yeah, this is kinda common - you’re loop assigns the onRelease to each clip, since the exist, but when the onRelease is triggered, it will only fire the last iteration.

to fix, this should work:

```auto

// I also datatyped and added a couple other "properisms"
for (var i:Number = 0; i < 12; i++) {
    var clip:MovieClip = mcBody.mainBody["thumbNail"+ i];
    clip.id = i;
    clip.onRelease = function() {
        _root.mcBody.mainBody.textCaption.textDisp = thumbNailCaption[this.id];
    }
}

```

there are much more proper ways to go about this - but, to get it working is the big thing. If you’re getting back into AS - look into things like the Delegate class in AS2 - its a nice thing to know of and understand prior to getting into as3 - hope this helps!

---

<div class="post-metadata">

**Author:** ![rumblesushi](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@rumblesushi](https://forum.kirupa.com/u/rumblesushi)\
**Post date:** [June 5, 2008, 1:29am UTC](https://forum.kirupa.com/t/for-loop-simple-question/262316/3 "2008-06-05T01:29:46Z")

</div>

Yep, that’s exactly what I do, works perfectly 😉

---

<div class="post-metadata">

**Author:** ![Soig](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Soig](https://forum.kirupa.com/u/Soig)\
**Post date:** [June 5, 2008, 2:17am UTC](https://forum.kirupa.com/t/for-loop-simple-question/262316/4 "2008-06-05T02:17:06Z")

</div>

Thanks, that did the trick. Considering how different our code was, I’m surprised that mine was at all functional! 🙂
