# Removing event listeners

**URL:** <https://forum.kirupa.com/t/removing-event-listeners/261293>\
**Category:** flash\
**Created:** [May 25, 2008, 9:15pm UTC](https://forum.kirupa.com/t/removing-event-listeners/261293 "2008-05-25T21:15:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alber\_Kidd](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Alber\_Kidd](https://forum.kirupa.com/u/Alber_Kidd)\
**Post date:** [May 25, 2008, 9:15pm UTC](https://forum.kirupa.com/t/removing-event-listeners/261293/1 "2008-05-25T21:15:11Z")

</div>

I have just found out I can do this

myMovie\_mc.addEventListener(MouseEvent.CLICK, function()  
{ parent.gotoAndPlay(1);

});

Up untill now I have been creating named functions, this will save a few lines of code, but how do I remove this event listener?

---

<div class="post-metadata">

**Author:** ![snickelfritz](https://avatars.discourse-cdn.com/v4/letter/s/7ea924/32.png) [@snickelfritz](https://forum.kirupa.com/u/snickelfritz)\
**Post date:** [May 26, 2008, 4:30am UTC](https://forum.kirupa.com/t/removing-event-listeners/261293/2 "2008-05-26T04:30:19Z")

</div>

Why would you want to remove the click listener?  
Just create weak references:

```auto
buttonContainer.addEventListener(MouseEvent.CLICK, action, false, 0, true);
buttonContainer.addEventListener(MouseEvent.MOUSE_OVER, action, false, 0, true);
buttonContainer.addEventListener(MouseEvent.MOUSE_OUT, action, false, 0, true);

var button:String = "";
function action(event:MouseEvent):void {
	button = event.target.name;
	switch (event.type) {
		case MouseEvent.MOUSE_OVER :
			//function here
			break;
		case MouseEvent.MOUSE_OUT :
			//function here
			break;
		case MouseEvent.CLICK :
			//function here
			break;
	}
}

```

If you wrap all of your button movieclips in “buttonContainer”, this all the code you will need for a virtually unlimited number of buttons.  
You can use the button instance name as a test for downstream switch conditionals, since it has been stored within the “button” variable.  
ie: if the variable button is equal to “btn1”, do something.  
It will be equal to the instance name of whichever button you click.

---

<div class="post-metadata">

**Author:** ![Alber\_Kidd](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Alber\_Kidd](https://forum.kirupa.com/u/Alber_Kidd)\
**Post date:** [May 26, 2008, 8:47am UTC](https://forum.kirupa.com/t/removing-event-listeners/261293/3 "2008-05-26T08:47:24Z")

</div>

That looks like an interesting technique but for now…

How do you remove the event listeners in this way. I know how to remove them when calling a function but I cannot get the syntax correct for this method.

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 26, 2008, 11:13am UTC](https://forum.kirupa.com/t/removing-event-listeners/261293/4 "2008-05-26T11:13:01Z")

</div>

U cant remove anonymous listener.  
But:

```auto
function action(event:MouseEvent):void {
   event.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, action);
}

```
