# How can I keep adding and removing the same content to container?

**URL:** <https://forum.kirupa.com/t/how-can-i-keep-adding-and-removing-the-same-content-to-container/260077>\
**Category:** flash\
**Created:** [May 12, 2008, 1:32pm UTC](https://forum.kirupa.com/t/how-can-i-keep-adding-and-removing-the-same-content-to-container/260077 "2008-05-12T13:32:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SasuS](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@SasuS](https://forum.kirupa.com/u/SasuS)\
**Post date:** [May 12, 2008, 1:32pm UTC](https://forum.kirupa.com/t/how-can-i-keep-adding-and-removing-the-same-content-to-container/260077/1 "2008-05-12T13:32:59Z")

</div>

Hello,  
I’m trying to make an order form that would be closed from a button. I know this is simple question, but how the odd thing is that when I press the close button named btn the first time everything works fine. The second time I open the form and close it I get an error message:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.  
at flash.display::DisplayObjectContainer/removeChild() at MethodInfo-994()

The third time I open and close the form I get the error two times. The fourth time I open and close the form I get the error four time etc… I think you get the idea… The problem must be somewhere in removing the child and then adding it again and removing. Can someone help me please? Here is the code:

var container:Sprite = new Sprite();  
addChild(container);  
var forma1:Loader = new Loader();  
var urlReq:URLRequest = new URLRequest(“file.swf”);  
forma1.load(urlReq);  
forma1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);

function loaded(event:Event):void  
{  
container.addChild(forma1);  
btn.addEventListener(MouseEvent.CLICK, closeforma);  
function closeforma(event:MouseEvent):void  
{  
container.removeChild(forma1); // I also tried removeChild(container) without any better luck…  
}  
}

---

<div class="post-metadata">

**Author:** ![SasuS](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@SasuS](https://forum.kirupa.com/u/SasuS)\
**Post date:** [May 13, 2008, 6:06am UTC](https://forum.kirupa.com/t/how-can-i-keep-adding-and-removing-the-same-content-to-container/260077/2 "2008-05-13T06:06:15Z")

</div>

Indeed, I had forgot to remove the listener… Thanks a lot amarghosh, that solved my problem!!!
