Connecting Tech Pros Worldwide Help | Site Map

Detach Dom Element

  #1  
Old January 22nd, 2006, 08:25 PM
Siah
Guest
 
Posts: n/a
Hi,

I'd like to detach some of my dom elements and cache them in an object.
My current options are to cache its innerHTML, or create an independent
'div' element and move the element as a child underneath that div
element. There should be a way for me to just detach my element from
document's dom tree and be able to set it into a variable.

Thanks,
Sia

  #2  
Old January 23rd, 2006, 12:05 AM
RobG
Guest
 
Posts: n/a

re: Detach Dom Element


Siah wrote:[color=blue]
> Hi,
>
> I'd like to detach some of my dom elements and cache them in an object.
> My current options are to cache its innerHTML[/color]

That is unlikely to be the best option - innerHTML is implemented
differently in different browsers so results will probably be
inconsistent, especially if you are dynamically modifying the element.

[color=blue]
> or create an independent
> 'div' element and move the element as a child underneath that div
> element. There should be a way for me to just detach my element from
> document's dom tree and be able to set it into a variable.[/color]

The removeChild method returns a reference to the removed element, so
keep that reference in a variable (global if it needs to survive beyond
the life of the function). You could add it to a div that has display =
'none' to store it in the document, but that seems unnecessary.

Here is a trivial example:

<script type="text/javascript">

// Object to store removed nodes. Use node ID as the key,
// reference to node as value.
var removedNodeStore = {};

function archiveNode(nodeID)
{
var n = document.getElementById(nodeID);
if (n && !(nodeID in removedNodeStore)){
removedNodeStore[nodeID] = n.parentNode.removeChild(n);
}
}

function restoreNode(nodeID, parentID)
{
if (nodeID in removedNodeStore) {
var p = document.getElementById(parentID);
p.appendChild(removedNodeStore[nodeID]);
delete removedNodeStore[nodeID];
}
}

</script>

<div>
<input type="button" value="Remove paragraph"
onclick="archiveNode('para01');">
<input type="button" value="Restore paragraph"
onclick="restoreNode('para01', 'div01');">
</div>
<div id="div01">
<p id="para01">Here is the <span style="color: red;">paragraph
</span> to remove.</p>
</div>


Check for support for getElementById before using it.


--
Rob
  #3  
Old January 23rd, 2006, 01:15 PM
Martin Honnen
Guest
 
Posts: n/a

re: Detach Dom Element




Siah wrote:

[color=blue]
> I'd like to detach some of my dom elements and cache them in an object.
> My current options are to cache its innerHTML, or create an independent
> 'div' element and move the element as a child underneath that div
> element. There should be a way for me to just detach my element from
> document's dom tree and be able to set it into a variable.[/color]

You don't need the div, removeChild e.g.
someParentNode.removeChild(someChildNode)
will remove the node someChildNode from the children of the node
someParentNode and return the removed node.
<http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247>

And in terms of the DOM if you wanted to store several nodes temporarily
you could use a document fragment to keep those nodes. But IE 5 does not
support document fragment nodes for HTML documents.

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #4  
Old January 23rd, 2006, 08:05 PM
Siah
Guest
 
Posts: n/a

re: Detach Dom Element


Thanks guys. removeChild is the magic I was looking for.

Sia

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript not working on my PC but ok on others drwyness answers 16 August 22nd, 2008 12:04 AM
IE 7 memory leak when you remove an element from page, or Drip bug? Sergei Shelukhin answers 4 October 25th, 2007 09:55 AM
Can I Avoid eval() Here? Good Man answers 6 November 24th, 2006 02:35 AM