• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / jQuery clone – Making a Copy of an Element

jQuery clone – Making a Copy of an Element

March 10, 2022 Leave a Comment

We can use the jQuery clone() method to copy an existing HTML element and add a copy of it to our HTML.

$("#div1").clone();

The jQuery clone() method would copy the div #div1 and any elements it contains.

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").clone();

Let’s take a look at an example.


Let’s say we have the following HTML:

<div id="div1">
  <p class="p1">This is some text.</p>
</div>
<div id="click-me1" class="click-me">Clone and add text above</div>

Let’s say we want to copy the paragraph above and add it to the div. We can do this easily with the jQuery clone() method. We can also use the appendTo() method to then add the cloned paragraph to the div.

Here is the JavaScript code:

$("#click-me1").click(function(){
  //We only want to clone the first p, so we use :first
  var clonedP = $(".p1:first").clone();
  clonedP.appendTo("#div1");
});

Here is the final code and output for this example:

Code Output:

This is some text.

Clone and add text above

Full Code:

<div id="div1">
  <p class="p1">This is some text.</p>
</div>
<div id="click-me1" class="click-me">Clone and add text above</div>


<script>
$("#click-me1").click(function(){
  var clonedP = $(".p1:first").clone();
  clonedP.appendTo("#div1");
});
</script>

Let’s take a look at another example below.

Using the jQuery clone() Method to Create a Bunch of Divs

In this simple example, we will have a div that will be a box with a greenish background. We will provide a button for the user be able to clone this div, and add its clone right next to it. Here is the HTML setup:


<style>.box{ float: left; width: 50px; height: 50px; background: #7bbfa2; margin-right: 10px; margin-bottom: 10px; }</style>
<div id="div2">
  <div class="box"></div>
</div><div class="clear"></div>
<div id="click-me2" class="click-me">Clone box</div>

In our function, we will clone the div, and then change its background color using the css() method.

We will finally add the new div using the appendTo() method like we did in the example above.

$("#click-me2").click(function(){
  var clonedP = $(".box:first").clone();
  //Create a random color
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  //Give the new box a random background color
  clonedP.css("background",randomColor);
  clonedP.appendTo("#div2");
});

The final code and output for this example of using the jQuery clone() method to create a bunch of divs is below:

Code Output:

Clone box

Full Code:


<style>.box{ float: left; width: 50px; height: 50px; background: #7bbfa2; margin-right: 10px; margin-bottom: 10px; }</style>
<div id="div2">
  <div class="box"></div>
</div><div class="clear"></div>
<div id="click-me2" class="click-me">Clone box</div>

<script>
$("#click-me2").click(function(){
  var clonedP = $(".box:first").clone();
  var randomColor = "#" + (Math.floor(Math.random()*16777215).toString(16));
  clonedP.css("background",randomColor);
  clonedP.appendTo("#div2");
});
</script>

Hopefully this article has been useful for you to understand how to use the jQuery clone() method to make a copy of an element.

Other Articles You'll Also Like:

  • 1.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 2.  Using jQuery to Increment Value on Click
  • 3.  Get the Height of a Div Using jQuery
  • 4.  jQuery contains – How to Check if a Paragraph Contains Specific Text
  • 5.  Using jQuery to Remove Class from an HTML Element
  • 6.  jQuery last() – Get the Last Element
  • 7.  jQuery keyup Method
  • 8.  Get Padding of an Element Using jQuery
  • 9.  jQuery parent – Get the Parent Element of a Div
  • 10.  jQuery mouseout – An Interactive Example of the mouseout Method

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy