Skip to content

Instantly share code, notes, and snippets.

@steida
Created July 15, 2010 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steida/477047 to your computer and use it in GitHub Desktop.
Save steida/477047 to your computer and use it in GitHub Desktop.
// 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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment