• 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
  • VBA
  • 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.  Using jQuery to Get the Margin of an Element
  • 2.  Examples of Text Animations Using jQuery
  • 3.  Using jQuery to Change the Id of a Div
  • 4.  Input Mask Phone Number Using jQuery
  • 5.  Using jQuery to Delete an Element
  • 6.  jQuery keydown Method
  • 7.  Using jQuery to Remove Class from an HTML Element
  • 8.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 9.  jQuery get first child
  • 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy