We all know and love the jQuery clone() method and it's ability to create a deep copy of an object. But, in many cases, there's a little bit more work that needs to be done after a deep copy has taken place, especially after that duplicated element has been introduced into the DOM.

  • If you are copying form elements, the clone() method will also duplicate the value of the form elements. In many cases, you will want to clear out those values.

    _newElement.find('input').val('');
     

  • If your original element contained any elements which had events bound to them, you will have to reinitialize those events on the copied objects. But, do so carefully. If you are going to blanket the binding of an event to a class of elements, make sure that the original element event does not get bound to the same function again. One way to ensure this doesn't happen is to unbind all events first:

    $('img').unbind('click');
       
    $('img').click( ... );

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *