Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:38:41+00:00 2026-06-02T19:38:41+00:00

I wish to clone the innerhtml of an element ( #clone ) and insert

  • 0

I wish to clone the innerhtml of an element (#clone) and insert it into another element (#newLocation).

The problem with using clone() is that I will have first delete the elements (if any) currently in #newLocation, then iterate over each element in #clone and append it to #newLocation. It’s not the end of the world, but I’m hoping for a simpler way.

Instead, I thought I’d use html(). Since this won’t preserve events, I would have to delegate the even using ON. I had thought this would work, however, it doesn’t.

Any suggestions what I am doing wrong? Also, think it would be more efficient to use the clone() solution even if I can get html() solution working?

EDIT: Just wrote my very first plugin: http://jsfiddle.net/Wnr65/ which seems to work. Good idea to use? Sure it could be written better.

$(document).ready(function(){
    $("#cloneIt").click(function(){$('#newLocation').cloneInnerHtml('clone').css('color', 'red');});
    $("#clone a").click(function(){alert("click");});
    $("#clone select").change(function(){alert("change");});
});

(function( $ ){
    $.fn.cloneInnerHtml = function( id ) {
        this.empty();
        var $t=this;
        $('#'+id).each(function() {$t.append($(this).clone(true));});
        return this;
    };
})( jQuery );


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Clone INNERHTML</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js" language="javascript"></script>
<script type="text/javascript">

$(document).ready(function(){
    $("#cloneIt").click(function(){$('#newLocation').html($('#clone').html());});

    //Only works on original clone
    $("#clone a").on("click", function(){alert("click");});
    $("#clone select").on("change", function(){alert("change");});

    //Doesn't work at all
    $("#newLocation a").on("click", function(){alert("click");});
    $("#newLocation select").on("change", function(){alert("change");});

});

</script>

</head>
<body>
<input type="button" value="Clone it" id="cloneIt" />

<p>Original clone is shown below</p>
<div id="clone">
<a href="#">Click Me</a>
<select><option>Hello</option><option>Goodby</option></select>
</div>
<hr />
<p>Insert new clones below.</p>
<div id="newLocation">
<p class="someOldElement">someOldElement</p>
</div>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:38:42+00:00Added an answer on June 2, 2026 at 7:38 pm

    ​It is normal that your code doesn’t react as you want. When you do

    $("#clone a").on("click", function() {
        alert("click");
    });
    

    it will bind click event to the elements which match the #clone a selector. Those elements are determined at the moment of the execution. In your case, this line bind the event to the first and only link present in the page.

    The same rule apply for the code

    $("#newLocation a").on("click", function() {
        alert("click");
    });
    

    but the difference here is that, at the moment of execution, there is no a element inside #newLocation, so the selection is empty and the handler is not bound at all.

    Then, when you do

    $('#newLocation').html( $('#clone').html() );
    

    it will get the HTML content from one element and copy it into another element, but it’s only about HTML content, so the event binding still the same as before the “copy operation”.

    The on method has different syntax, and only one allow the event delegation:

    // get all current "a" elements inside the "#clone"
    // and bind the click event
    $( "#clone a" ).on( "click", handler );
    
    // get the "#clone" element, bind a click event to it
    // BUT trigger the event only when the origin of the event is an "a" element
    // here the event delegation is enabled
    // it means that future "a" elements will trigger the handler
    $( "#clone" ).on( "click", "a", handler );
    
    // if you want it to work with your code
    // you could do something like below
    // this add a click handler to all present and future "a" elements
    // which are inside "#clone" or "#newLocation"
    // but this is not the recommended way to do this, check below the clone method
    $("#clone, #newLocation").on("click", "a", handler);
    

    clone method doesn’t remove the elements, here is a working demo http://jsfiddle.net/pomeh/G34SE/

    HTML code

    <div class="source">
        <a href="#">Some link</a>
    </div>
    <div class="target">
    </div>​
    

    CSS code

    div {
        width: 100px;
        height: 25px;
    }
    
    .source {
        border: solid red 1px;
    }
    .target {
        border: solid blue 1px;
    }
    

    Javascript code

    var $element = $("a"),
        $target = $(".target"),
        $clone;
    
    
    $element.on("click", function( ev ) {
        ev.preventDefault();
        console.log("click");
    });
    
    
    $clone = $element.clone( true );
    
    $clone.appendTo( $target );​
    

    The method also receive a parameter indicating whether event handlers should be copied along with the elements http://api.jquery.com/clone/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pop up form that i wish to close on successfull submit
I wish to contribute code to http://svn.r-project.org/R/ But I only have experience with using
I have a problem that has been consuming me for days. I have a
I wish to programatically clone a widget. I am able to clone the Element
I wish to add an element to the form body which is a clone
I have have a simple script that I do not wish to close (exit)
I have a SQLite database that I wish to read records from and execute
I have a generic list of objects in C#, and wish to clone the
I have created a small command that will let me launch Internet Explorer. However,
I wish to send an email from my localhost machine (using PHPs mail function)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.