tags:

views:

38

answers:

3

When passing the contents of a <div> to another <div> with jquery as below, the original is removed. Is there a way to do this and still retain the original?
Example Here - I want the document to read Item One, Item Two, Item One.

   function display() {
    $('#display').html($('#ItemOne'));
}

display();
+4  A: 

use clone()

http://docs.jquery.com/Clone

function display() {
    $('#display').html($('#ItemOne').clone());
}

display();
js1568
This would work, but you should modify the ID if you're going to take this approach, so you don't end up with 2 elements with the same ID. Something like: `$('#ItemOne').clone().attr('id','cloned_content')`
patrick dw
+2  A: 
 $('#display').html($('#ItemOne').html());

add .html()

Patricia
I think OP wants the entire element (as far as I can tell). Using `.html()` would only append the content of `#ItemOne`. :o)
patrick dw
Actually the content is all I need (should have specified) so this works equally well.
jack
+1 @jack - If the content is all you need, than this is actually better. Remember that if you use `.clone()` you're also cloning the ID, which means you'll have 2 items on the page with the same ID. This could cause problems. Using `.html()` as done here would take care of that issue.
patrick dw
After some playing around I realised as such.Thanks for confirming!
jack
glad i could help :)
Patricia
+1  A: 

Change to

function open() {
    $('#display').html($('#ItemOne').clone());
}

See: jsfiddle

Topera