# Dynamic Button call

**URL:** <https://forum.kirupa.com/t/dynamic-button-call/281262>\
**Category:** flash\
**Created:** [February 6, 2009, 5:31pm UTC](https://forum.kirupa.com/t/dynamic-button-call/281262 "2009-02-06T17:31:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonlijw](https://avatars.discourse-cdn.com/v4/letter/j/f6c823/32.png) [@jasonlijw](https://forum.kirupa.com/u/jasonlijw)\
**Post date:** [February 6, 2009, 5:31pm UTC](https://forum.kirupa.com/t/dynamic-button-call/281262/1 "2009-02-06T17:31:10Z")

</div>

I am building a grid of buttons dynamically in a for loop from XML. I am then assigning them button.label and button.name from the array. The grid appears, the buttons have the right label, the event.target.name method produces the name on the button in a trace. Now how do I attach that to a function dynamically? I need the name of the button to call the next step.

Code looks like this so far.

import flash.display.Sprite;  
import fl.controls.Button;  
import flash.events.MouseEvent;

var jwPremURL:URLRequest = new URLRequest(“jwPrem.xml”);  
var xmlLoader:URLLoader = new URLLoader(jwPremURL);  
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

var jwPremXML:XML = new XML();  
jwPremXML.ignoreWhitespace = true;

var posY:Number = 50;  
var wTypeSelected:String = new String();  
var typeButtonName:String = new String();

function xmlLoaded(evt:Event):void{  
jwPremXML = XML(xmlLoader.data);

//Set array for window type  
var wType:Array = new Array(“SLIDER”,“SINGLE-HUNG”,“PICTURE”,“CASEMENT”,“AWNING”,“PATIO-DOOR”,“GEO-RADIUS”);

//Add type buttons  
for(var i:int =0;i \< wType.length; i++){  
var typeButton:Button=new Button();  
typeButton.scaleX=1.5;  
typeButton.scaleY=1.5;  
typeButton.x=20;  
typeButton.y=posY;  
typeButton.label = wType\*;  
typeButton.name = wType\*;  
addChild(typeButton);  
typeButtonName=typeButton.name  
posY = posY+40;  
trace (typeButtonName);

}  
}  
this.addEventListener(MouseEvent.CLICK, onClick);  
function onClick(event:MouseEvent):void  
{  
// TRACES THE NAME OF THE BUTTON CLICKED  
trace(event.target.name, ‘clicked’);  
}

Any help would be great.
