# Trying to pass a variable using addEventListener

**URL:** <https://forum.kirupa.com/t/trying-to-pass-a-variable-using-addeventlistener/300217>\
**Category:** flash\
**Created:** [November 17, 2009, 8:03pm UTC](https://forum.kirupa.com/t/trying-to-pass-a-variable-using-addeventlistener/300217 "2009-11-17T20:03:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![matrix8369\_yaho](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@matrix8369\_yaho](https://forum.kirupa.com/u/matrix8369_yaho)\
**Post date:** [November 17, 2009, 8:03pm UTC](https://forum.kirupa.com/t/trying-to-pass-a-variable-using-addeventlistener/300217/1 "2009-11-17T20:03:48Z")

</div>

Here is what I am trying to do. Make a dynamic number of buttons on the stage. Place a BtnNum variable in each button so that when the button is clicked it will pass that BtnNum to a function. Here is my code so far

var BtnAmout = 5;  
var Spacer = 5;

for(var i:Number=0;i\<=BtnAmout;i++){  
//I set Btn0-5 to the Btn movieclip in the lybrary  
this[“Btn”+i] = new Btn();  
//I space each Btn out onto the stage  
this[“Btn”+i].x = Spacer+(this[“Btn”+i].width_i)+(Spacer_i);  
this[“Btn”+i].y = Spacer;  
//I place each Btn onto the stage  
addChild(this[“Btn”+i]);  
//I create and set a variable BtnNum to the current number in the for loop  
this[“Btn”+i].BtnNum = i;  
//I have a text field in the Btn so I know what number the button it  
this[“Btn”+i].NumText.text = i;  
//Btn on click send the BtnNum variable to SayNumber to be traced out  
this[“Btn”+i].addEventListener(MouseEvent.CLICK, SayNumber(i));  
}

function SayNumber(i:String){  
trace("Btn Clicked was "+i)  
}

I know that if I use something like  
this[“Btn”+i].addEventListener(MouseEvent.CLICK, function(e:MouseEvent){SayNumber(e, “Hello”)});  
it will pass a variable but I want to make the variable dynamic and I am lost. I am new to as3 since a week ago. Any help would be amazing.
