Solved

How to copy a table using Javascript

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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