# Add remove EventListener array

**URL:** <https://forum.kirupa.com/t/add-remove-eventlistener-array/327452>\
**Category:** Uncategorized\
**Created:** [March 1, 2012, 6:15pm UTC](https://forum.kirupa.com/t/add-remove-eventlistener-array/327452 "2012-03-01T18:15:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonassus](https://avatars.discourse-cdn.com/v4/letter/b/ea5d25/32.png) [@bonassus](https://forum.kirupa.com/u/bonassus)\
**Post date:** [March 1, 2012, 6:15pm UTC](https://forum.kirupa.com/t/add-remove-eventlistener-array/327452/1 "2012-03-01T18:15:51Z")

</div>

I have an array of buttons in a public var in \_model.btnArray. I want to make a function that i can pass an index number parameter that will add addEventListener to the button at that index and removeEventListener to all other buttons in the array. The function creates a tempArray:Array and assigns \_model.btnArray to it. Then it addEventListener to the index passed as a parameter to the function. Then it splices the passed index out of the tempArray and then loops through spliced tempArray to remove all the EventListeners from it. which should be the listeners on all the buttons except for that one spliced. but this isn’t working, its giving me an error and i don’t know why.

should this be working or is it just wrong?

THANKS!!!

```auto
function addRemoveEventListener(num:Number){

            var tempArray:Array = _model.btnArray;  
            tempArray[num].addEventListener(MouseEvent.CLICK,onBtn);

            tempArray.splice(num,1);
            
            for (var i:int = 0; i < tempArray.length; i++) 
            {
               
                tempArray*.removeEventListener(MouseEvent.CLICK,onBtn);
            }
   
        }

```
