Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to copy a table using Javascript

Posted on 2012-04-03
2
Medium Priority
?
295 Views
Last Modified: 2012-04-03
Hello,

I need your help.

I was curious to know, how I would go about copying only a specified table (all rows and all columns) using Javascript:

Below is an example of my table:

<html>

<head>

</head>

<body>

<table border="1" cellspacing="1" width="200">
	<tr>
		<td>Fruits</td>
		<td>Vegetables</td>
		<td>Colors</td>
		<td>Numbers</td>
	</tr>
	<tr>
		<td>Apple</td>
		<td>Carrot</td>
		<td>Red</td>
		<td>10</td>
	</tr>
	<tr>
		<td>Orange</td>
		<td>Broccoli</td>
		<td>Blue</td>
		<td>20</td>
	</tr>
	<tr>
		<td>Pear</td>
		<td>Onions</td>
		<td>Green</td>
		<td>30</td>
	</tr>
</table>

</body>

</html>

Open in new window


Any help with this is greatly appreciated.

Thanks in advance,

Cheers,

J
0
Comment
Question by:Buggs2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 total points
ID: 37802181
Give the table an id : <table id="mytable" border="1" cellspacing="1" width="200">

If you want to copy both data and code then you grab it with

somevar=document.GetElementById('mytable').innerHTML;

if you just want the data:
rows=document.getElementById('mytable').rows;
for (1=0;i<rows.length;i++)
{
   cells=rows[1].cells;
   for (j=0;j<cells.length;j++)
   {
      // do something with the data
   }
}



Cd&
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37802677
Or document.getElementById('mytable').textContent

In any case getElement and not GetElement
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question