473,388 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Detach Dom Element

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

Jan 22 '06 #1
3 7906
Siah wrote:
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
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.

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.


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
Jan 23 '06 #2


Siah wrote:

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.


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/
Jan 23 '06 #3
Thanks guys. removeChild is the magic I was looking for.

Sia

Jan 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DBA | last post by:
If I detach a database on a server, then attach the database with a single file only using the data file...will I lose any transactions that were in the original log file? There is a debate going...
0
by: Ramesh K | last post by:
Hi All, Here is the scenario where i am having the problem. There is a COM component let us say "A" is running under DLLHOST . "A" creates normal COM component "B". To debug the component B i...
2
by: Bob | last post by:
Sometimes I get some odd design-time exceptions that are useful to troubleshoot by attaching the debugger to devenv.exe itself and breaking on CLR exceptions... works well but when I'm done I'd...
4
by: Esben Stien | last post by:
Any examples how to detach and run as a daemon?. I've seen some use System.out.close() and System.err.close(), but I just can't get it to detach and run as a daemon. I'm on GNU/Linux, using...
1
by: Barry Mossman | last post by:
Hi, is it possible to detach an event which is implemented via an anonymous method. ie. If the code is: myObject.myEvent += delegate(object sender, EventArgs e) { // snip event actions } is...
2
by: =?Utf-8?B?Um9nZWxpbw==?= | last post by:
so I notice to detach a database you use Server svr = new Server(); svr.DetachDatabase("MyDatabase"); but is there a way you can set where its saved after detaching ? thansk
1
by: RP | last post by:
I am using SQL Server 2005 with C# 2005. I want to programmatically Detach a database, then copy its files from the data folder to another location and then Attach the database back. Can it be done...
2
by: Marina | last post by:
We have MDI WinForms C# .Net2 application. Parent form has MdiContainer=true; Children forms are created like this: Form child = new Form(); child.MdiParent = this; child.Show(); To...
9
by: dpatel75 | last post by:
I am trying to copy a database from a SQL 2000 SP3 Windows 2000 server to a 2005 SP2 Windows 2003 server. I am trying to use detach and attach method (have tried both within Management Studio and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.