How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Duplicate a DIV into Another DIV with jQuery
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Duplicate a DIV into Another DIV with jQuery
Web Development

How to Duplicate a DIV into Another DIV with jQuery

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
Duplicate a div into another div with jQuery clone
SHARE

To duplicate a div into another div with jQuery, use .clone() to get a copy of the element and an insertion method like .appendTo() or .html() to place it into the target. For the common case of a popup or preview that mirrors some source content, .clone() is the cleaner choice.

Contents
  • Starting markup
  • Option 1 — .clone() (recommended)
  • Option 2 — .html() (string copy)
  • Cloning with event handlers
  • Strip or rewrite ids on the clone
  • Vanilla JS equivalent
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with jQuery 3.7. Originally published 2022-10-26, rewritten and updated 2026-05-17.

Starting markup

<div class="content">
    <h1>Title</h1>
    <p>Some Content</p>
</div>

<div class="popup">
    <!-- duplicate goes here -->
</div>

Option 1 — .clone() (recommended)

var elem = $('.content').clone();
$('.popup').html(elem);

.clone() returns a duplicate jQuery object that’s detached from the DOM. .html(elem) replaces the inner content of .popup with it, including the outer <div class="content"> wrapper.

jQuery duplicate div — clone vs html, with/without events, deep id handling

Option 2 — .html() (string copy)

var elem = $('.content').html();
$('.popup').html(elem);

This copies only the inner markup of .content (the <h1> and <p>, not the wrapping div). Same .html() method is doing two different things here: with no argument it’s a getter that returns a string; with an argument it’s a setter that replaces inner HTML.

Cloning with event handlers

// Shallow clone — copies markup, drops event handlers
$('.content').clone();

// Deep clone — copies jQuery data + events on the cloned element
$('.content').clone(true);

// Deeper still — events on the element AND all its descendants
$('.content').clone(true, true);

Default is structure-only. Pass true when you want a true behavioural copy.

Strip or rewrite ids on the clone

var cloned = $('.content').clone();

// Strip every id from the clone and its descendants
cloned.find('[id]').addBack('[id]').removeAttr('id');

$('.popup').html(cloned);

HTML id attributes must be unique per page. Cloning a subtree that contains ids and inserting it elsewhere will silently duplicate them — labels stop pointing at the right input, CSS rules match more than one element. Strip them or rewrite them with a fresh prefix on the clone.

Vanilla JS equivalent

const source = document.querySelector('.content');
const target = document.querySelector('.popup');

// Element clone
target.innerHTML = '';
target.appendChild(source.cloneNode(true));

// Or: string copy (equivalent of .html())
target.innerHTML = source.innerHTML;

Frequently asked questions

Should I use .clone() or .html() to copy a div?

.clone() returns an element (with all of its descendants, attributes, and optionally its event handlers); .html() returns the inner HTML as a string. If you want a full structural copy with the outer wrapper, use .clone(). If you only need the inner markup re-injected, .html() is fine and slightly cheaper.

Does .clone() copy event handlers too?

Only when you pass true: $('.content').clone(true) includes attached jQuery data and event handlers; $('.content').clone(true, true) also copies handlers on every descendant. Without the argument, only DOM structure and attributes are copied — useful when you want a clean copy that won’t fire stale handlers.

How do I avoid duplicate id attributes when cloning?

id values must be unique per page. After cloning, walk the new tree and either strip ids (cloned.find('[id]').addBack('[id]').removeAttr('id')) or rewrite them to a fresh prefix. Forgetting this is a common cause of subtle bugs — labels stop linking to the right input, CSS rules match more than one element, etc.

Is there a vanilla JS equivalent?

Yes: node.cloneNode(true) clones the element with all descendants, and target.append(clone) inserts it. target.innerHTML = source.innerHTML is the string-copy equivalent of .html(). Both work without jQuery on every modern browser.

Related guides

  • How to Check if an Element Is Visible or Hidden with jQuery
  • How to Check if a Checkbox Is Checked with jQuery
  • How to Disable or Enable an Input with JavaScript or jQuery

References

jQuery .clone(): api.jquery.com/clone. jQuery .html(): api.jquery.com/html. Node.cloneNode(): developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode.

TAGGED:domJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Disable and enable a form input with JavaScript and jQuery How to Disable or Enable an Input with JavaScript or jQuery
Next Article Enable the PHP DOM extension on a Linux server How to Enable the PHP DOM Extension
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

Comment in a .gitignore file with the # character
Web Development

How to Comment in a .gitignore File

4 Min Read
Check if GD library is installed in PHP (phpinfo and extension_loaded)
Web Development

How to Check if GD Library Is Installed in PHP (3 Easy Methods)

5 Min Read
WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter
Web Development

How to Dynamically Change Currency in WooCommerce

7 Min Read
Fix Laravel CSRF token mismatch in DataTables Ajax
Web Development

How to Fix Laravel CSRF Token Mismatch in DataTables Ajax

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?