views:

2345

answers:

5

Hi,

i am looking for some tipps how to solve my problem.

I habe a html element (like select box input field) in a table. Now i want to copy the object and generate a new one out of the copy, and that with JavaScript or jQuery. I think this should work somehow but i´m a little bit clueless at the moment.

Something like this (pseudo code):

oldDdl = $("#ddl_1").get();

newDdl = oldDdl;

oldDdl.attr('id', newId);

oldDdl.html();

+2  A: 

Look at the clone() method of JQuery

Using your code you can do something like that:

$('#ddl_1').clone().attr('id',newId).appendTo('p'); // append to where you want
Boris Guéry
+2  A: 

It's actually very easy in jQuery:

$("#ddl_1").clone().attr("id",newId).appendTo("body");

Change .appendTo() of course...

Philippe Leybaert
+1 you beat me!
DrJokepu
A: 

In one line:

$('#selector').clone().attr('id','newid').appendTo('#newPlace');
DrJokepu
+3  A: 

With native javascript:

newelement = element.cloneNode(bool)

where the boolean indicates whether to clone child nodes or not

annakata
A: 

Thank you guys (all of you), worked flawlessly.

Richard
You shouldn't say thank you with an answer, accepting the answer and vote up is enough ! You're welcome, anyway.
Boris Guéry