# Using variable in place of element in array to addeventlisteners

**URL:** <https://forum.kirupa.com/t/using-variable-in-place-of-element-in-array-to-addeventlisteners/280209>\
**Category:** flash\
**Created:** [January 24, 2009, 6:49am UTC](https://forum.kirupa.com/t/using-variable-in-place-of-element-in-array-to-addeventlisteners/280209 "2009-01-24T06:49:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![punstc](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@punstc](https://forum.kirupa.com/u/punstc)\
**Post date:** [January 24, 2009, 6:49am UTC](https://forum.kirupa.com/t/using-variable-in-place-of-element-in-array-to-addeventlisteners/280209/1 "2009-01-24T06:49:10Z")

</div>

This is probably something very simple that I just can’t seem to get to work. Basically I’m trying to use a variable to be called inside of an array instead of the actual array so a simple example would be

```auto

var people:Array = [john,tom,ann];
var selected = john;
trace(people[selected]);

```

I get an error everytime I try to use a variable in place of and actual element

This is how i’m trying to use it…

I have an array that i’m adding event listeners to for a navigation.

```auto

var buttons:Array=[productBtn,useBtn,stylesBtn,contactBtn];

for (var i:int=0; i<buttons.length; i++) {
	buttons*.buttonMode=true;
	buttons*.addEventListener(MouseEvent.MOUSE_OVER, over);
	buttons*.addEventListener(MouseEvent.MOUSE_OUT, out);
	buttons*.addEventListener(MouseEvent.CLICK, goToPage);
}

```

inside the click function “gotoPage” I want to save a variable that takes the last clicked button and re-adds eventlisteners which would be removed to show the active state of the current page.

So when you click it removes the event-listeners and takes the button to its active state. I want to use the current variable to re-add those events for the button and then save the new current or last clicked.

```auto

function goToPage(e:MouseEvent):void {
	var current = stylesBtn ; //setting up initial variable
	
	if (e.currentTarget==productBtn) {
		trace("you hit the product button");
	
		buttons[0].removeEventListener(MouseEvent.MOUSE_OVER, over);
		buttons[0].removeEventListener(MouseEvent.MOUSE_OUT, out);
		buttons[0].removeEventListener(MouseEvent.CLICK, goToPage);
		buttons[0].gotoAndStop(5);
		buttons[current].addEventListener(MouseEvent.MOUSE_OVER, over); //trying to use the current in array
		buttons[current].addEventListener(MouseEvent.MOUSE_OUT, out);
		buttons[current].addEventListener(MouseEvent.CLICK, goToPage);
                current = productBtn; //saving new current for next button click
	} 
}

```

Like I said this is probably something simple and my brain isn’t working but I appreciate the help. thanks alot.
