Questions tagged [jquery-clone]

A jQuery method to create a deep copy of the set of matched elements

The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes.

When used in conjunction with one of the insertion methods, .clone() is a convenient way to duplicate elements on a page.

Example

Consider the following HTML:

<div class="container">
    <div class="hello">Hello</div>
    <div class="goodbye">Goodbye</div>
</div>

As shown in the discussion for .append(), normally when an element is inserted somewhere in the DOM, it is moved from its old location. So, given the code:

$('.hello').appendTo('.goodbye');

The resulting DOM structure would be:

<div class="container">
    <div class="goodbye">
        Goodbye
        <div class="hello">Hello</div>
    </div>
</div>

To prevent this and instead create a copy of the element, you could write the following:

$('.hello').clone().appendTo('.goodbye');

This would produce:

<div class="container">
    <div class="hello">Hello</div>
    <div class="goodbye">
        Goodbye
        <div class="hello">Hello</div>
    </div>
</div>

Note:
When using the .clone() method, you can modify the cloned elements or their contents before (re-)inserting them into the document.
Normally, any event handlers bound to the original element are not copied to the clone. The optional withDataAndEvents parameter allows us to change this behavior, and to instead make copies of all of the event handlers as well, bound to the new copy of the element. As of jQuery 1.4, all element data (attached by the .data() method) is also copied to the new copy.

However, objects and arrays within element data are not copied and will continue to be shared between the cloned element and the original element. To deep copy all data, copy each one manually:

var $elem = $('#elem').data( "arr": [ 1 ] ), // Original element with attached data
    $clone = $elem.clone( true )
    .data( "arr", $.extend( [], $elem.data("arr") ) ); // Deep copy to prevent data sharing

As of jQuery 1.5, withDataAndEvents can be optionally enhanced with deepWithDataAndEvents to copy the events and data for all children of the cloned element.

Note:
Using .clone() has the side-effect of producing elements with duplicate id attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using class attributes as identifiers instead.

Resource

69 questions
12
votes
3 answers

Clone Element without Children

Is there a way to copy an element without copying it's children? My goal is to duplicate a table, along with all classes, inline styles, etc. But I do not want to copy any of the table element's children. I realize I could copy the entire table and…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
5
votes
2 answers

How do I clone element events excluding data in jQuery?

I am trying to clone the html div element with class ".age-sex-details" and the events bound to the ".age-sex-remove" and ".age-sex-add" classes shown below using the jquery clone function. I am able to clone the div, the events mentioned above and…
ysfiqbl
  • 396
  • 3
  • 17
4
votes
1 answer

How can I clone a node, copying the data but not the event handlers?

jQuery's clone function looks like this: .clone( [withDataAndEvents ] [, deepWithDataAndEvents ] ) withDataAndEvents: A Boolean indicating whether event handlers and data should be copied along with the elements. deepWithDataAndEvents: A…
Shawn
  • 10,931
  • 18
  • 81
  • 126
3
votes
2 answers

using jquery remove part of a cloned element

having a difficult time removing a div inside of a cloned element. run the snippet and notice the do not clone me part gets appended even though it is removed. let myhtml = `
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
3
votes
2 answers

Change name of radio button upon jquery clone()

Right here is my HTML::
Attributes
user3130415
  • 363
  • 3
  • 6
  • 11
2
votes
2 answers

jquery clone and add event

I'm trying to clone a "div" and all inside it but one of the events disappear in the cloned div (I'm pretty new to web developing so ... I "cloned" a code found on SO). The missing event is added with the following javascript: var MyClass =…
genespos
  • 3,211
  • 6
  • 38
  • 70
2
votes
2 answers

How to increment name attrVal array on cloned input using jQuery?

I'm trying to setup an RSVP form for my wedding whereby you can clone a couple of fields to RSVP for more than one guest at a time (http://adrianandemma.com/). These cloned fields include a couple of radio buttons for 'attending Yes/No'. When I…
ade123
  • 2,773
  • 8
  • 29
  • 32
2
votes
1 answer

how to make Jquery draggable element append to droppable when dropped

Edit: I want the final cloned element to be draggable to any position in the droppable, i do not want it to be sortable. I'm trying to append my draggable-clone element to droppable element when it is dropped. when the draggable element is inside…
2
votes
0 answers

How to destroy() specific element and re-initialize on clone using jQuery

I have the following issiue: Im using the slim image cropping plugin for my application, but have some issiues when cloning the the html and re-initialize the plugin. I am destroying the object in order to get the plugin to work after i clone the…
sdfgg45
  • 1,232
  • 4
  • 22
  • 42
2
votes
2 answers

How to rename all id in a cloned form (javascript)?

I have a form (commonStuff) cloned this way (and an html page, designed in jquery mobile, where the form appears multiple times, cloned): var commonClone = commonStuff.cloneNode(true); and this function function renameNodes(node) { var i; …
Sasha Grievus
  • 2,566
  • 5
  • 31
  • 58
2
votes
0 answers

On Drop - Clone a Different node or corresponding Element from the server

On the drop (after drag) -- how can I clone a different (corresponding) element that resides somewhere else or back on the server, i.e. target and clone a different element on the server For e.g. On client side the corresponding icons map to the…
aroos
  • 505
  • 2
  • 5
  • 12
2
votes
0 answers

How to stop jQuery element from being cloned when dragged?

I'm a designer who's relatively new to jQuery, so please bear with me! I've put together a database query creation program based on a jQuery shopping cart model. Essentally, the user drags field names and boolean operators to a "cart" area and drops…
wordtom
  • 21
  • 1
2
votes
3 answers

How to clone an HTML block?

I'm struggling with the Jquery clone. I can clone a single line of HTML, but I don't have a clue on how to clone a block. I have this block: