views:

1016

answers:

3
+3  Q: 

clone table row

+5  A: 

You can hookup a live event to all the buttons. If you give them a class of clone for instance the following will work.

$('input.clone').live('click', function(){
   //put jquery this context into a var
   var $btn = $(this);
   //use .closest() to navigate from the buttno to the closest row and clone it
   var $clonedRow = $btn.closest('tr').clone();
   //append the cloned row to end of the table

   //clean ids if you need to
   $clonedRow.find('*').andSelf().filter('[id]').each( function(){
       //clear id or change to something else
       this.id += '_clone';
   });

   //finally append new row to end of table
   $btn.closest('tbody').append( $clonedRow );
});

Please Note: If you have elements in the table row with id's you will need to do a .each through them and set them to a new value otherwise you will end up with duplicate id's in the dom which is not valid and can play havoc with jQuery selectors

You can do this like so

redsquare
Im not smart enough to fill in your comment with the actual action of appending the new row into the table. We are half the way there.
Patrick
$(this).closest('tbody').append($clonedRow); should stick it at the bottom of the table body.
Joel Potter
As it stands, they dont have individual IDs, so i should be ok as far as that goes?
Patrick
cool, no worries then, I added the code if you need to clean them up
redsquare
Note that the id cleaning still leaves you with duplicate ids if you clone the same row twice.
Rodrigo Queiro
Yes rodrigo....its not a full solution but I take it that the OP has to do some work!
redsquare
A: 

If you want a really simple solution, just use innerHTML:

var html = document.getElementById("the row").innerHTML;

var row = document.createElement('p');

row.innerHTML= html;

document.getElementById("table id").appendChild(row);
CrazyJugglerDrummer
may look simple but a) how does it hookup to the button clicks, how does it deal with duplciate id's basically all the issues the OP will face?
redsquare
Yeah, im a baby give it to me in a bottle, lol.
Patrick
A: 

For what purpose do you want to use the data? I've done similar things previously on data input forms and generally I've found it to be beneficial to the users not to manipulate everything in Javascript but to hook to store the data on the server and interface with AJAX.

The issue is that as soon as you start letting users do this sort of complex table manipulation and they accidentally hit the back button you end up with a lot of disgruntled punters. Coding up transient storage on a database isn't that much harder than manipulating Javascript and in fact can be easier as you can break the operations down more easily. Debugging is simpler for that reason too (you always have inspect access to the current state of your table).

Lots of option for handling via AJAX - the simplest being to just use a place-holder division and feed the whole table structure in as needed.

Cruachan
It will be only for 1 or 2 users. Input will come from either a CSV, or MYSQL table, which will then be placed into the main page as editable entries (not like a db editor, more of like a template for additional rows), user can edit existing elements or duplicate and edit more before submitting the updated information.
Patrick