# Removing EventListeners using a for loop

**URL:** https://forum.kirupa.com/t/removing-eventlisteners-using-a-for-loop/326851
**Category:** Uncategorized
**Created:** [January 16, 2012, 10:19pm UTC](https://forum.kirupa.com/t/removing-eventlisteners-using-a-for-loop/326851 "2012-01-16T22:19:28Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisW182](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@ChrisW182](https://forum.kirupa.com/u/ChrisW182)
#### Post date: [January 16, 2012, 10:19pm UTC](https://forum.kirupa.com/t/removing-eventlisteners-using-a-for-loop/326851/1 "2012-01-16T22:19:28Z")

</div>

I have 20 buttons on my stage, and rather then have ‘removeEventListener’ for each button, i wanted to use a loop to do so. Heres what i have done.

```auto

//array containing the buttons instance names.
var soundArray:Array = new Array();
var buttonsOnArray:Array = new Array();

//event listener for the button i wish to turn off once its selected.
mainHold.Bass1_mc.addEventListener(MouseEvent.CLICK, soundSelected);

//this function is called once the button is clicked, so i want it to disable whatever button was clicked

function soundSelected(evt:MouseEvent):void{
    soundArray.push(evt.target.fileName);//pushes the filename into a playlist array.
    buttonsOnArray.push(evt.target.name);//pushes the instance names of the buttons 'on'.
    trace ("PlayList = "+soundArray);
    trace ("Buttons On are ="+buttonsOnArray);
    //trace (evt.target.name);
    
    for (var i:int = 0; i < buttonsOnArray.length; i++)
    {
        buttonsOnArray*.removeEventListener(MouseEvent.CLICK, soundSelected);

    }
}

```

However this doesnt seem to work, it still keeps the button as active, basically each button adds a filename (that i have assigned to it) to a playlist array (soundArray), but i only want it to add it once.

Please help im going mad.

Thanks.
