Solved

How to copy a table using Javascript

Posted on 2012-04-03
2
288 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

839 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