# AS3 - Dynamic listener and handler function

**URL:** <https://forum.kirupa.com/t/as3-dynamic-listener-and-handler-function/272037>\
**Category:** flash\
**Created:** [October 1, 2008, 2:58pm UTC](https://forum.kirupa.com/t/as3-dynamic-listener-and-handler-function/272037 "2008-10-01T14:58:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![anyuser](https://avatars.discourse-cdn.com/v4/letter/a/f1d935/32.png) [@anyuser](https://forum.kirupa.com/u/anyuser)\
**Post date:** [October 1, 2008, 2:58pm UTC](https://forum.kirupa.com/t/as3-dynamic-listener-and-handler-function/272037/1 "2008-10-01T14:58:05Z")

</div>

Hi guys,  
AS3 thing, heaving problems with getting this thing to work.  
Basically I am trying to create a dynamic function ie. the dynamic name of a function.

I am loading xml file with links in it ex. \<link\_01\>url\</link\_01\>…  
then for loop, where a button (acctually a mc, but behaves as one) for each url is created:

ActionScript3 Code:

```auto
for (var i:uint = 0; i<xmlData.*.length(); i++)
linkButton = new linkButton_mc();
linkButton.name = "linkButton_"+ (i + 1);

var inst:String = linkButton_001.name;

```

next I want to add to each of those mcs a listener, so in this same iteration. (It needs to be dynamic, so I found out earlier that you need to use [] to use a string as a dynamic variable name, so that each dynamically created button gets its own dynamic listener)

ActionScript3 Code:

```auto
[inst]addEventListener(MouseEvent.MOUSE_OVER,overFunction);
function boldFunction(e:MouseEvent):void{
     [inst]overShape_mc.visible = false;
     [inst]outShape_mc.visible = true;

     [inst]addEventListener(MouseEvent.MOUSE_OUT,regularFunction);
     function regularFunction (e:MouseEvent):void{
          [inst]overShape_mc.visible = true;
          [inst]outShape_mc.visible = false;

     };
};
]

```

But now I am getting only the last button to display roll over/out states, even when I roll over the first button the roll over state shows up only at the last button.

I tried to make those handler functions dynamic:  
ActionScript3 Code:

```auto
var overFunction:String = "on"+inst+"_Over";
var outFunction:String = "on"+inst+"_Out";

[inst]addEventListener(MouseEvent.MOUSE_OVER, [overFunction]);
function [overFunction](e:MouseEvent):void{
     [inst]overShape.visible = false;
     [inst]outShape.visible = true;
....

```

…but the [] trick doesn’t work. What I get is:  
**1084: Syntax error: expecting identifier before leftbracket.**

Is there any way of creating dynamic functions? Or do you have any other ideas to go round the need of using dynamic function names?  
cheers
