jQuery clone() Method

The jQuery clone() method can be used to make a copy of the selected element. The copies content also includes the element child nodes, texts, and attributes. It is useful when you want to create clones of the elements.

jQuery clone() Method

Syntax

The syntax of this method is given below:-

Clone Selected Element
$(selector).clone()
Copy Selected Element Including Event Handler
$(selector).clone(true)

Description of the Parameters

The description of the parameters is given below.

Parameter Name Description
selector You have to specify the element to select and make a clone. Use tag name, class name, or id of the elements to select.
true Specify the boolean value to including the event handler when copying the element.

jQuery clone() Method to Copy Selected Element

If you want to make copies of the selected element without including the event handlers, you can use the example given below. The example creates the clone of the paragraph element until you click the button.

Example

Test it Live

Output

This is my paragraph.

When you click the button given above, it creates a copy of the element and appends it to the div element. You can create multiple copies by clicking the element for more than one time.

Copy an Element Including Event Handler

You can also add events with the copies elements like animation and other effects. To perform this task, you have to pass ‘true’ as the parameter of the jQuery clone() method.

Example

Test it Live

Output

This is my paragraph.

Click the button given above to make the clone of the selected element. The clone also contains the animation effect of the original element. If you click the paragraph, it animates the clone and increases the height of the selected element.

You may also like to read

I hope you like this tutorial on jQuery clone() method. If you have any queries regarding the tutorial, please comment below.

References