The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> how to copy table td tags from one table row to other table row
kashaboina
post Dec 28 2009, 12:04 AM
Post #1





Group: Members
Posts: 1
Joined: 27-December 09
Member No.: 10,695



Hi,

I have 2 tables:

<table id=tbl1>
<tr id=tbl1_tr1>
<td id=tbl1_td1>
something……
</td>
</tr>
</table>

<table id=tbl2>
<tr id=tbl2_tr1>

</tr>
</table>

Now I want to copy ‘tbl1_td1’ to ‘tbl2_tr1’ row through javascript.

1q: Is it possible?
2q: If yes, how?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 28 2009, 05:56 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,648
Joined: 10-August 06
Member No.: 7



This is quite possible with the DOM.

To copy a cell, use the cloneNode() method. To insert it into the row you can use appendChild() (puts it after any existing cells) or insertBefore() (puts it before a specified existing cell). See http://www.quirksmode.org/dom/w3c_core.html#nodemanipulation for details.

Keep in mind that adding or removing cells may break the table structure. To avoid that you might begin with a static number of cells, and use replaceChild() to replace an empty one in the second table with a clone of the cell in the first.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Dec 28 2009, 08:02 AM
Post #3


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Will that get the attributes too? If so, there will be duplicate IDs.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 28 2009, 09:26 AM
Post #4


.
********

Group: WDG Moderators
Posts: 9,648
Joined: 10-August 06
Member No.: 7



Attributes will indeed be copied! You might edit their value or simply remove the whole attribute in the copy.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 28 2009, 05:55 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,648
Joined: 10-August 06
Member No.: 7



Another perhaps more robust idea is to copy just the content of the first table's cell into a pre-existing empty cell in the second table. The innerHTML property makes this simple.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 16th April 2024 - 11:04 AM