# How to pass arguments from an event listener in AS3 - SIMPLE

**URL:** <https://forum.kirupa.com/t/how-to-pass-arguments-from-an-event-listener-in-as3-simple/305293>\
**Category:** flash\
**Created:** [February 22, 2010, 11:42pm UTC](https://forum.kirupa.com/t/how-to-pass-arguments-from-an-event-listener-in-as3-simple/305293 "2010-02-22T23:42:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidbarile](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@davidbarile](https://forum.kirupa.com/u/davidbarile)\
**Post date:** [February 22, 2010, 11:42pm UTC](https://forum.kirupa.com/t/how-to-pass-arguments-from-an-event-listener-in-as3-simple/305293/1 "2010-02-22T23:42:48Z")

</div>

In AS2, I often found it useful to pass arguments to a function on a button press like so:

[COLOR=Blue]btn.onRelease = function(){  
doSomething(“arg1”, “arg2”, “arg3”);  
}[/COLOR]

I searched the interweb for DAYS trying to find how to accomplish this via an event listener in AS3, but everything I found suggested creating a custom Event. The problem with this, is you have to decide the arguments you wish to pass in advance, and I wanted to create them dynamically on the fly ( for example: passing a string that a user entered into a textfield to some function for processing ).

So last night I spent hours scouring forums - and UREKA! I finally found something that I was able to tweak to send a variable number of arguments of any data type. I’ve never posted on a forum, but it was such a breakthru that I had to share it with the world. I hope you find it as useful as I do.

Here is the code:

[COLOR=Blue]btn1.addEventListener( MouseEvent.CLICK, function onClick(e:Event ) { displayArgs(e, new Array(“Larry”, “Curly”, “Moe”) ) } );

btn2.addEventListener( MouseEvent.CLICK, function onClick(e:Event ) { displayArgs(e, new Array(“John”, “Paul”, “George”, “Ringo”) ) } );  
[COLOR=Silver]  
// this can be any function that needs to take zero or more parameters[/COLOR]  
function displayArgs(e:Event, arg:Array){  
trace(e.currentTarget + " args = ");  
for ( var i:uint = 0; i \< arg.length; ++i )  
{  
trace( arg\* );  
}  
}[/COLOR]
