SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Shropshire, England
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    removing document.createElement element

    I've written the following small feature test.

    Code:
    var _delete = (function(el, prop){
      try {
        var div = document.createElement('div'); div._xyz = 1;	 
        delete(div._xyz);
        return function(el, prop){ delete el[prop] };
      } catch(e){
        return function(el,prop){ el.removeAttribute(prop) };
      }
    }());
    I want to remove 'div' before returning the function. Testing in IE something like div.parentNode.removeChild(div); will fail. If I look in the dom 'div's parentNode is null, so that explains that. It needs to be appended to something first I guess.

    What's the best way of going about this?

    I guess I could append a unique Id to the body, but I'm trying to avoid that.

    How about simply delete div; ?

    Thanks

    RLM

  2. #2
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Shropshire, England
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I temporarily use the body this seems to work okay. Would like to know how to go down the document.createElement route.

    Code JavaScript:
    var _delete = (function(el, prop){
      try {
    	var body = document.body, tmpId = '_tmpId:' + new Date();	
    	body[tmpId] = 1;
    	delete body[tmpId]; // try this
    	return function(el, prop){ delete el[prop] };
      } catch(e){
    	body.removeAttribute(tmpId);
        return function(el,prop){ el.removeAttribute(prop) };
      }
    }());
     
    alert(_delete.toString());

    RLM


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •