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
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Create a folder in PHP if it does not already exist
Web Development

How to Create a Folder If It Does Not Exist in PHP

5 Min Read
Set A4 paper size in CSS for printing
Web Development

How to Set A4 Paper Size in CSS for Print

5 Min Read
WordPress login user programmatically — wp_set_current_user plus wp_set_auth_cookie
Web Development

How to Login a User Programmatically in WordPress

8 Min Read
Laravel DataTables custom column search — filterColumn callback handles the search SQL
Web Development

How to Search Custom or Composite Columns in Laravel DataTables

8 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?