Bug Tracker

Ticket #6793 (closed enhancement: invalid)

Opened 4 years ago

Last modified 3 years ago

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.

 http://gist.github.com/477047

Change History

comment:1 Changed 4 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
				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;
};

comment:2 Changed 4 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?

comment:3 Changed 4 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 4 years ago by trac-o-bot

  • Status changed from pending to closed
  • Resolution set to invalid

Automatically closed due to 14 days of inactivity.

Note: See TracTickets for help on using tickets.