473,394 Members | 1,774 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,394 software developers and data experts.

Dom copy table

hello,

I have the follwoing code

<div id="detailtable" style="visibility:hidden">
<table id="detailtablehidden" width="100%" border="0">
....
</table>
</div>

I'm trying to copy the table inside to another table by appending it to
a td tag.

...
var the_table = document.getElementById("detailtable").innerHTML;
var td_content = document.createTextNode(the_table);
td.appendChild(td_content);

The problem right now is that I'm getting the table inserted as Text
instead of html. the follwing is an example. Please help
&lt;table _vpps_id=&quot;65&quot; id=&quot;detailtablehidden&quot;
border=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;thead&gt; &lt;tr
_vpps_id=&quot;66&quot;&gt; &lt;td _vpps_id=&quot;67&quot;&gt;Student
Name &lt;/td&gt; &lt;td
_vpps_id=&quot;68&quot;&gt;PaymentAmount&lt;/td&gt; &lt;td
Thankks

Jul 23 '05 #1
2 14100
Liming wrote:
hello,

I have the follwoing code

<div id="detailtable" style="visibility:hidden">
<table id="detailtablehidden" width="100%" border="0">
....
</table>
</div>

I'm trying to copy the table inside to another table by appending it to
a td tag.

..
var the_table = document.getElementById("detailtable").innerHTML;
var td_content = document.createTextNode(the_table);
td.appendChild(td_content);
You are mixing DOM with proprietary (though widely copied) MS
functionality. You should use either one or the other - preferably
DOM, particularly as there are issues with innerHTML and tables.

The following are purely examples, take note of issues. When
copying/cloning elements, you must be careful not to end up with
invalid HTML. One particular issue is that you must ensure that
duplicated elements get unique ids if you have an ID on the element
being cloned and it will remain in the page.

DOM
You can only append a table to another element that is allowed to
have a table inside it - div, body, etc.

You can 'clone' an entire element very simply, it makes an exact copy
so if you clone something with an id and don't remove the original, you
will have duplicate ids which is not valid and may cause issues.

var the_table = document.getElementById(t);
var newTable = the_table.cloneNode(true);
newTable.id = 'visibleTable';
document.body.appendChild(newTable);

The big advantage of DOM is that you can append the table easily to any
existing element, it doesn't have to be the body, or any new element
you create. You can access the properties of the new element directly
- it's easy to change the id. We can also access the elements inside
the table too - rows, cells, etc. and their attributes.

innerHTML
innerHTML will replace the HTML inside an element. For a table, you
need to put it inside something like a div, so create one and put the
copied table in there:

var the_table = document.getElementById(t).innerHTML;
var oDiv = document.createElement('div');
document.body.appendChild(oDiv);
oDiv.innerHTML = the_table;

The disadvantage with innerHTML is that it totally replaces the
content of the element it is 'append' to. This can be a short-cut when
you want to replace the content, but it's a bit clumsy if you don't -
you have to create a new 'container' to put it in.

It is also much harder to access the properties of the cloned node and
its children - try changing the id of the table, or the content of one
of the cells.

innerHTML is really handy for simple replacement of element content,
but not very good if any kind of other processing is required. It
can't be used to replace parts of a table in IE (say replace or add a
row or cell).

The problem right now is that I'm getting the table inserted as Text
instead of html. the follwing is an example. Please help
&lt;table _vpps_id=&quot;65&quot; id=&quot;detailtablehidden&quot;
border=&quot;0&quot; width=&quot;100%&quot;&gt; &lt;thead&gt; &lt;tr
_vpps_id=&quot;66&quot;&gt; &lt;td _vpps_id=&quot;67&quot;&gt;Student
Name &lt;/td&gt; &lt;td
_vpps_id=&quot;68&quot;&gt;PaymentAmount&lt;/td&gt; &lt;td


Whatever you insert with innerHTML is parsed by the browser and
rendered. Insert text and that's what you'll get.
--
Rob
Jul 23 '05 #2
Rob,

oh my god, thank you so much.

I didn't expect anyone to give me such a great detailed reply plus such
a great mini-tutorial, it sure helped me cleared up a few concepts.

I'm saving this piece. lol.

thanks again. I followed your code and bang, everythign works as it is
suppose to.

Thanks again.

Lim

Jul 23 '05 #3

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

Similar topics

2
by: news.hp.com | last post by:
I have situation where I need to copy multiple records (only certain fields) from a Rules table to an Events table based on a selection identified in a combo box. When the selection is made in a...
3
by: Tlm | last post by:
Hello All, I have a form (FrmA) with a subform (SubFrmB) embedded in it. SubFrmB also has a subform embedded in it (SubFrmC) The form's recordsource is based on a table (TblA). SubFrmB's...
19
by: davidgordon | last post by:
Hi, I need some pointers/help on how to do the following if it possible: In my access db, I have the following: Tables: Products, Sub-Assembly, Product-Pack Table, Products
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
2
by: Janning Vygen | last post by:
hi PGurus, i searched the archives and read the docs, because this problem shouldn't be new. But i really don't know what to search for. i am populating a database (v7.4.1) with COPY. Some...
2
by: Clodoaldo Pinto Neto | last post by:
Hi all, I'm trying to copy a table with a text field column containing a new line char to a file: ksDesenv=# create table page(line text) without oids; CREATE TABLE ksDesenv=# insert into...
2
by: Marcin Zmyslowski | last post by:
Hello all! How to copy a table in MS SQL Server 2000 without chaning a structure? I mean, I have one table, which has autoincrement numeric field (ID). When I copy this table by exporting this...
2
by: Swinky | last post by:
I hope someone can help...I feel like I'm walking in the dark without a flashlight (I'm NOT a programmer but have been called to task to do some work in Access that is above my head). I have...
4
by: Jim Devenish | last post by:
I wish to copy a table on a SQL Server backend to a table on an Access (.mdb) front end in the simplest way. I have the following to get the recordset but am seeking something easier. Dim...
9
by: fniles | last post by:
I would like to copy a table(s) from SQL Server 2005 to a CVS file and vice versa. I was thinking to use the BCP command line utility, but I have a few questions: 1. The machine where I am...
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?
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.