Solved

How to copy a table using Javascript

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

751 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