back to Overview

.clone()

Clones all HTML nodes in the given list by creating a deep copy of them.
Web module only.

Syntax

list.clone()

Parameters

(return value)
the list containing copies of all supported items in the original list.

Description

Clones all HTML nodes in the given list by creating a deep copy of them. Nested lists will be automatically flattened. Everything else will be copied as-is into the new list.

clone() uses the browser's cloneNode() function to clone HTML internally, but will remove the ids from all HTML top-level elements. This allows you to specify an element to clone by id without creating duplicate ids in the document. The ids of child elements will removed.

Please note that clone() does work with SVG, but will not remove ids from SVG.

Please note that event handlers will not be cloned.

Example

Using the following HTML:

<div id="comments">
   <div id="comment1">My comment.</div>
</div>
Creating a clone:
var myClone = $('#comment1').clone();
Adding it below the original:
$('#comments').add(myClone);

See also..

  • add() can add a cloned element to the HTML document.

Comments

comments powered by Disqus

back to Overview

Functions