Bug Tracker

Opened 13 years ago

Closed 12 years ago

Last modified 11 years ago

#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:
Blocked by: Blocking:


Its pretty old fix from hedgerwang, rewamped for jQuery.


Change History (4)

comment:1 Changed 13 years ago by dmethvin

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
				// this method merge all attributes except events
				// 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'
				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;

comment:2 Changed 12 years ago by snover

Owner: set to steida
Priority: undecided
Status: newpending

Could you please explain what specific issues this code is addressing?

comment:3 Changed 12 years ago by anonymous

Read the comments you moron.

"IE copies events bound via attachEvent when cloneNode."

Instead of innerHTML, use clearAttributes.

comment:4 Changed 12 years ago by trac-o-bot

Resolution: invalid
Status: pendingclosed

Automatically closed due to 14 days of inactivity.

Note: See TracTickets for help on using tickets.