Fully destroy an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShishKabab
    New Member
    • Jun 2007
    • 7

    Fully destroy an object

    Hello Everyone,

    I have two functions:
    Code:
    function purgeNode(node){
    	while(node.childNodes.length > 0){
    		var childNode = node.firstChild;
    		
    		if(childNode.childNodes.length > 0)
    			purgeNode(childNode);
    		
    		node.removeChild(childNode);
    		childNode = null;
    		delete childNode;
    	}
    	
    	node.parentNode.removeChild(node);
    	node = null;
    	delete node;
    }
    
    function purgeContent(){
    	var content = getObj("content");
    	purgeNode(content);
    }
    I have a text field which is in the content div:
    Code:
    <input type="text" id="txtCustomer" />
    I enter some text(bla) into txtCustomer and do an Ajax request. The response calls purgeContent() and fills the content with some other stuff and a textfield txtCustomer. I type another word in the textfield(test) . When I now look at the value of txtCustomer through document.getEle mentById("txtCu stomer").value it says that the value is still the old one(bla) :S. It appears that the first textfield is somehow not entirely destroyed. How do I fully destroy it?

    Kind Regards,
    Vincent
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    function purgeContent(){
    var content = document.getEle mentById("conte nt");
    while(content.l astChild)conten t.removeChild(c ontent.lastChil d);
    }

    This method removes all the childNodes of content.

    In IE, an input element has no child nodes, so it isn't getting removed by your node method. If you add another input with the same id the first one defined is returned by document.getEle mentById.

    Your use of delete doesn't do anything- delete is used to remove a property of an object, or an element of an array. If it is called on something that is not an object, an error is supposed to be called, but IE sometimes throws in the window- delete window.node returns true, even if there is no window.node defined.

    Comment

    • ShishKabab
      New Member
      • Jun 2007
      • 7

      #3
      I see. So is there any way to achieve my goal? I really don't want to write a lot of extra PHP code just because of some old object hanging around.

      Comment

      Working...