473,382 Members | 1,365 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,382 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 7905
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.