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

  • SEARCH
  • Home
  • 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 7657153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:57:07+00:00 2026-05-31T12:57:07+00:00

The following code fails to properly clone the input element. Clicking/focusing on the cloned

  • 0

The following code fails to properly clone the input element. Clicking/focusing on the cloned input opens the datepicker on the original input.

http://jsfiddle.net/musicisair/YUkZw/

<input>
<script>
    var input = $("input").datepicker();
    $("body").append("<br>Input 2: ").append(
        input.clone(true);
    );
</script>

Is there a correct way of cloning elements that have been bound with a jQuery UI widget? If so, what is is?

  • 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-05-31T12:57:09+00:00Added an answer on May 31, 2026 at 12:57 pm

    Normally, any event handlers bound to the original element are not
    copied to the clone. The optional withDataAndEvents parameter allows
    us to change this behavior, and to instead make copies of all of the
    event handlers as well, bound to the new copy of the element. As of
    jQuery 1.4, all element data (attached by the .data() method) is also
    copied to the new copy.

    However, objects and arrays within element data are not copied and
    will continue to be shared between the cloned element and the original
    element. To deep copy all data, copy each one manually:

    var $elem = $('#elem').data( "arr": [ 1 ] ), // Original element with attached data
        $clone = $elem.clone( true )
        .data( "arr", $.extend( [], $elem.data("arr") ) ); // Deep copy to prevent data sharing
    

    As of jQuery 1.5, withDataAndEvents can be optionally enhanced with
    deepWithDataAndEvents to copy the events and data for all children of
    the cloned element.

    Source: http://api.jquery.com/clone/

    I believe you are looking for the code above that actually copies the data associated with an element rather than sharing the data between elements.

    Update

    After messing around a few minutes with this here’s what I came up with:

    //create original datepicker
    var $input = $("input").datepicker(),
    
    //clone the datepicker, copy the data from the original, and change the ID of the new element
        $clone = $input.clone(true).data( "datepicker", $.extend( true, {}, $input.data("datepicker") ) ).attr('id', 'test-id');
    
    //now change the references in the data for the clone to point to the clone
    $clone.data('datepicker').input = $clone;
    $clone.data('datepicker').id = 'test-id';
    
    
    //add the clone to the DOM
    $("body").append("<br><br><br>Input 2: ").append(
        $clone
    );
    ​
    

    And a demo: http://jsfiddle.net/YUkZw/5/

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

Sidebar

Related Questions

The following code fails in 'Evaluate' with: This expression was expected to have type
The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace
The following code fails to run on Python 2.5.4: from matplotlib import pylab as
Can anyone tell me why the following code fails to submit the form into
I can't figure out why the following code fails: # test.ps1 `$args: ($args) `$args
I have the following code that fails when i run it. my .h file:
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following code snippet on SQL server 2005 fails on the ampersand '&': select
If I use strict mode the following code does not work. It fails on
The following code fails throws a Zend_Controller_Exception (Invalid value passed to setPost(); must be

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.