# Event listeners into arrays/loops help!

**URL:** <https://forum.kirupa.com/t/event-listeners-into-arrays-loops-help/246812>\
**Category:** Uncategorized\
**Created:** [December 14, 2007, 7:00pm UTC](https://forum.kirupa.com/t/event-listeners-into-arrays-loops-help/246812 "2007-12-14T19:00:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrutter](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/jrutter/32/2004_2.png) [@jrutter](https://forum.kirupa.com/u/jrutter)\
**Post date:** [December 14, 2007, 7:00pm UTC](https://forum.kirupa.com/t/event-listeners-into-arrays-loops-help/246812/1 "2007-12-14T19:00:51Z")

</div>

I have been working on this for about 2 hrs, Im trying to think of a way to compress all the code I have so far into about 3 loops/arrays.

I have 20 instances of the same button that will be added to the stage, call it car\_btn. Then I have an xml file where Im grabbing the nodes using an index number ex: [3]. Then I have 20 instances of the same popup\_mc, all of the buttons pull data from an xml file and populate this popup\_mc.

here is my code, anyone know how to simplify all of this into a series of arrays/loops. I want to make it so its not adding just one instance of the popInfo - but one for each button that is clicked.

```auto

// variables
var popInfo:popMc;

// load xml
var xml:XML = new XML();
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("data/cities.xml"));
loader.addEventListener(Event.COMPLETE,
    function(evt:Event):void {
        xml = XML(evt.target.data);
        //trace(xml.city[0].@weburl.toString());
        var url:URLRequest = new URLRequest(xml.city[0].@weburl.toString()); 

    }
);

 

// event listeners
seattleBtn.addEventListener(MouseEvent.CLICK, onClickLoadData);
losangelesBtn.addEventListener(MouseEvent.CLICK, onClickLoadData);
stage.addEventListener(MouseEvent.CLICK, onClickStageRemove);

// functions
function onClickLoadData(event:MouseEvent):void
{
    if(event.target == seattleBtn)
    {
    popInfo = new popMc();
    popInfo.x = 130;
    popInfo.y = 50;
    addChild(popInfo);    
    popInfo.closeBtn.addEventListener(MouseEvent.CLICK , onClickRemove);
    popInfo.nyopBtn.addEventListener(MouseEvent.CLICK, onClickNYOP);
    popInfo.carsizeText.text = xml.city[1].bidlist.carsize.descendants().toXMLString();
    popInfo.priceText.text = xml.city[1].bidlist.price.descendants().toXMLString();
    popInfo.savingsText.text = xml.city[1].bidlist.savings.descendants().toXMLString();
    popInfo.stateText.text = xml.city[1].@name.toString();    
    }
    else if(event.target == losangelesBtn)
    {
    popInfo = new popMc();
    popInfo.x = 100;
    popInfo.y = 50;
    addChild(popInfo);    
    popInfo.closeBtn.addEventListener(MouseEvent.CLICK , onClickRemove);
    popInfo.nyopBtn.addEventListener(MouseEvent.CLICK, onClickNYOP);
    popInfo.carsizeText.text = xml.city[2].bidlist.carsize.descendants().toXMLString();
    popInfo.priceText.text = xml.city[2].bidlist.price.descendants().toXMLString();
    popInfo.savingsText.text = xml.city[2].bidlist.savings.descendants().toXMLString();
    popInfo.stateText.text = xml.city[2].@name.toString();    
    }
}

    
function onClickRemove(event:MouseEvent):void
{
    removeChild(popInfo);
}

function onClickStageRemove(event:MouseEvent):void
{
    if(event.eventPhase == EventPhase.AT_TARGET) {
        removeChild(popInfo);
    }
}

function onClickNYOP(event:MouseEvent):void 
{
    //navigateToURL(url, "_blank");
    // navigateToURL(urlNY, "_blank"); //trying to get this variable to work
    ExternalInterface.call("selectNY");

}

// set button mode here
seattleBtn.buttonMode = true;
losangelesBtn.buttonMode = true;

```
