# Issue with variable inside a function

**URL:** <https://forum.kirupa.com/t/issue-with-variable-inside-a-function/221226>\
**Category:** Uncategorized\
**Created:** [April 2, 2007, 5:30pm UTC](https://forum.kirupa.com/t/issue-with-variable-inside-a-function/221226 "2007-04-02T17:30:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsj4](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tsj4](https://forum.kirupa.com/u/tsj4)\
**Post date:** [April 2, 2007, 5:30pm UTC](https://forum.kirupa.com/t/issue-with-variable-inside-a-function/221226/1 "2007-04-02T17:30:12Z")

</div>

I have a for loop that is adding button functionality to 16 buttons on the stage. It is targeting the button fine with no problem using the var i as the button number. The issue is inside the button function I have another nested button function for the on state of the button which I also use the var i to specify the url number I want associated with the particular button but it is always outputting 17. I don’t even understand how it is getting that number. Here is the code.

```auto

function gotoURL(movieNum) {
	getURL("javascript:flashURL("+movieNum+");");
}
// ----
for (i=0; i<=16; i++) {
	this["b"+i].onRelease = function() {
		// -- bring selected button to the top deptht         
		_root.x += 2;
		this.swapDepths(_root.x);
		// -- defines last selected button and then delete it         
		var lastBtn:MovieClip;
		this._parent.lastBtn.gotoAndPlay(11);
		trace("Button Event One");
		this.gotoAndPlay(2);
		delete this._parent.lastBtn.onPress;
		this._parent.lastBtn = this;
		updateAfterEvent();
		// -- If the user clicks the on state then it goes to some URL         
		this.onPress = function() {

			_root.gotoURL(i);             
			trace (i);

			trace("Button Event two");
			updateAfterEvent();
		};
	};
} 

```
