tadam logo
Did you find an error in the text?
Select this with mouse and press
Ctrl + Enter
Xhtml.co.il Check Spelling
Orphus system

.clone( [ withDataAndEvents ] )

Returns: jQuery

Description: Create a deep copy of the set of matched elements.

.clone( [ withDataAndEvents ], [ deepWithDataAndEvents ] )

version added: 1.5
withDataAndEvents
A Boolean indicating whether event handlers and data should be copied along with the elements. The default value is false. *For 1.5.0 the default value is incorrectly true. This will be changed back to false in 1.5.1 and up.
deepWithDataAndEvents
A Boolean indicating whether event handlers and data for all children of the cloned element should be copied. By default its value matches the first argument's value (which defaults to false).

.clone( [ withDataAndEvents ] )

version added: 1.0
withDataAndEvents
A Boolean indicating whether event handlers should be copied along with the elements. As of jQuery 1.4 element data will be copied as well.

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. 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 we insert an element 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, we 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 that when using the .clone() method, we 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.

Examples

Example 1

Clones all b elements (and selects the clones) and prepends them to all paragraphs.
$("b").clone().prependTo("p");
The output of the code above will be:

Example 1 - Full source:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
  <script>
  $(document).ready(function(){
$("b").clone().prependTo("p");
  });
  </script>
</head>
<body>
  <b>Hello</b><p>, how are you?</p>
</body>
</html>

Example 2

Create a button that's able to clone itself - and have the clones themselves be clonable.
    $("button").click(function(){
      $(this).clone(true).insertAfter(this);
    });
The output of the code above will be:

Example 2 - Full source:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>  
  <script>
  $(document).ready(function(){
    $("button").click(function(){
      $(this).clone(true).insertAfter(this);
    });
  });
  </script>
</head>
<body>
  <button>Clone Me!</button>
</body>
</html>
Was this information helpful?
   

Comments