Solved

How to copy a table using Javascript

Posted on 2012-04-03
2
285 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
2 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
Comment Utility
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
Comment Utility
Or document.getElementById('mytable').textContent

In any case getElement and not GetElement
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now