Modify ↓
Ticket #6793 (closed enhancement: invalid)
jQuery clone without innerHTML workaround for IE
| Reported by: | steida | Owned by: | steida |
|---|---|---|---|
| Priority: | undecided | Milestone: | 1.4.3 |
| Component: | core | Version: | 1.4.2 |
| Keywords: | clone cloneNode ie bug | Cc: | |
| Blocking: | Blocked by: |
Description
Its pretty old fix from hedgerwang, rewamped for jQuery.
Change History
comment:2 Changed 3 years ago by snover
- Owner set to steida
- Priority set to undecided
- Status changed from new to pending
Could you please explain what specific issues this code is addressing?
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

Code from the link:
// jQuery clone fix for Internet Explorer events clonning issue, added deep option // daniel.steigerwald.cz 2010, MIT licence jQuery.fn.clone = function (events, deep) { // Do the clone var ret = this.map(function () { if (!jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this)) { // IE copies events bound via attachEvent when // using cloneNode. Calling detachEvent on the // clone will also remove the events from the orignal // In order to get around this, we use innerHTML. // Unfortunately, this means some modifications to // attributes in IE that are actually only stored // as properties will not be copied (such as the // the name attribute on an input). var clone = this.cloneNode(!!deep), froms = [this].concat(deep ? this.getElementsByTagName('*') : []), tos = [clone].concat(deep ? clone.getElementsByTagName('*') : []); for (var i = tos.length; i--; ) { var to = tos[i], from = froms[i]; // this method remove attached events to.clearAttributes(); // this method merge all attributes except events to.mergeAttributes(from); // retain selected options if (from.options) { var toOptions = to.options, fromOptions = element.options; for (var j = no.length; j--; ) toOptions[j].selected = fromOptions[j].selected; } // fix some browsers inconsistenses var attr = { input: 'checked', option: 'selected', textarea: 'value' }[this.tagName.toLowerCase()]; if (attr && from[prop]) to[prop] = attr[prop]; }; return clone; } else { return this.cloneNode(!!deep); } }); // Copy the events from the original to the clone if (events === true) { cloneCopyEvent(this, ret); cloneCopyEvent(this.find("*"), ret.find("*")); } // Return the cloned set return ret; };