Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

108 lines (107 sloc) 5.9 kB
<?xml version="1.0"?>
<entry type="method" name="clone" return="jQuery">
<title>.clone()</title>
<signature>
<added>1.0</added>
<argument name="withDataAndEvents" optional="true" type="Boolean" default="false">
<desc>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.</desc>
</argument>
</signature>
<signature>
<added>1.5</added>
<argument name="withDataAndEvents" optional="true" type="Boolean" default="false">
<desc>A Boolean indicating whether event handlers and data should be copied along with the elements. The default value is <code>false</code>. <em>*In jQuery 1.5.0 the default value was incorrectly <code>true</code>; it was changed back to <code>false</code> in 1.5.1 and up.</em></desc>
</argument>
<argument name="deepWithDataAndEvents" optional="true" type="Boolean" default="value of withDataAndEvents">
<desc>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 <code>false</code>).</desc>
</argument>
</signature>
<desc>Create a deep copy of the set of matched elements.</desc>
<longdesc>
<p>The <code>.clone()</code> method performs a <em>deep</em> 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, <code>.clone()</code> is a convenient way to duplicate elements on a page. Consider the following HTML:</p>
<pre><code>&lt;div class="container"&gt;
&lt;div class="hello"&gt;Hello&lt;/div&gt;
&lt;div class="goodbye"&gt;Goodbye&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>As shown in the discussion for <code><a href="http://api.jquery.com/append/">.append()</a></code>, normally when an element is inserted somewhere in the DOM, it is moved from its old location. So, given the code:</p>
<pre><code>$('.hello').appendTo('.goodbye');</code></pre>
<p>The resulting DOM structure would be:</p>
<pre><code>&lt;div class="container"&gt;
&lt;div class="goodbye"&gt;
Goodbye
&lt;div class="hello"&gt;Hello&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</code></pre>
<p>To prevent this and instead create a copy of the element, you could write the following:</p>
<pre><code>$('.hello').clone().appendTo('.goodbye');</code></pre>
<p>This would produce:</p>
<pre><code>&lt;div class="container"&gt;
&lt;div class="hello"&gt;Hello&lt;/div&gt;
&lt;div class="goodbye"&gt;
Goodbye
&lt;div class="hello"&gt;Hello&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</code></pre>
<blockquote>
<p><strong>Note:</strong> When using the <code>.clone()</code> method, you can modify the cloned elements or their contents before (re-)inserting them into the document.</p>
</blockquote>
<p>Normally, any event handlers bound to the original element are <em>not</em> copied to the clone. The optional <code>withDataAndEvents</code> 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 <code>.data()</code> method) is also copied to the new copy. </p>
<p>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:</p>
<pre><code>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
</code></pre>
<p>As of jQuery 1.5, <code>withDataAndEvents</code> can be optionally enhanced with <code>deepWithDataAndEvents </code> to copy the events and data for all children of the cloned element.</p>
<blockquote>
<p><strong>Note:</strong> Using <code>.clone()</code> has the side-effect of producing elements with duplicate <code>id</code> attributes, which are supposed to be unique. Where possible, it is recommended to avoid cloning elements with this attribute or using <code>class</code> attributes as identifiers instead.</p>
</blockquote>
</longdesc>
<example>
<desc>Clones all b elements (and selects the clones) and prepends them to all paragraphs.</desc>
<code><![CDATA[
$("b").clone().prependTo("p");
]]></code>
<html><![CDATA[
<b>Hello</b><p>, how are you?</p>
]]></html>
</example>
<example>
<desc>When using <code>.clone()</code> to clone a collection of elements that are not attached to the DOM, their order when inserted into the DOM is not guaranteed. However, it may be possible to preserve sort order with a workaround, as demonstrated:</desc>
<css><![CDATA[
#orig, #copy, #copy-correct {
float: left;
width: 20%;
}
]]></css>
<code><![CDATA[
// sort order is not guaranteed here and may vary with browser
$('#copy').append($('#orig .elem')
.clone()
.children('a')
.prepend('foo - ')
.parent()
.clone());
// correct way to approach where order is maintained
$('#copy-correct')
.append($('#orig .elem')
.clone()
.children('a')
.prepend('bar - ')
.end());
]]></code>
<html><![CDATA[
<div id="orig">
<div class="elem"><a>1</a></div>
<div class="elem"><a>2</a></div>
<div class="elem"><a>3</a></div>
<div class="elem"><a>4</a></div>
<div class="elem"><a>5</a></div>
</div>
<div id="copy"></div>
<div id="copy-correct"></div>
]]></html>
</example>
<category slug="manipulation/copying"/>
<category slug="version/1.0"/>
<category slug="version/1.5"/>
</entry>
Jump to Line
Something went wrong with that request. Please try again.