← Back to Index

.clone()

Create a deep copy of the set of matched elements.

Examples

1. Basic Clone

// <div class="original">Hi</div>
const $clone = $('.original').clone();
$clone.addClass('copy').appendTo('body');

Result

<div class="original">Hi</div>
<div class="original copy">Hi</div>

2. Clone with Data and Events

const $smartClone = $('.btn').clone(true);

Result

// Returns a new SR object with elements that have
// the same event listeners as the original .btn elements.

Parameters

Parameter Type Description
withDataAndEvents boolean Optional. If true, copies event handlers and data for the element itself. Default: false.
deepWithDataAndEvents boolean Optional. If true, copies event handlers and data for all children of the cloned element. Defaults to the value of the first argument.

Returns: A new SR object containing the cloned elements.