Created
June 24, 2014 20:04
-
-
Save colelawrence/16878253f1bed1ded465 to your computer and use it in GitHub Desktop.
[Stackoverflow Answer](http://stackoverflow.com/a/24395071/2096729)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.extend({ | |
replaceTag: function (element, tagName, withDataAndEvents, deepWithDataAndEvents) { | |
var newTag = $("<" + tagName + ">")[0] | |
// From [Stackoverflow: Copy all Attributes](http://stackoverflow.com/a/6753486/2096729) | |
$.each(element.attributes, function() { | |
newTag.setAttribute(this.name, this.value); | |
}); | |
$(element).children().clone(withDataAndEvents, deepWithDataAndEvents).appendTo(newTag) | |
return newTag | |
} | |
}) | |
$.fn.extend({ | |
replaceTag: function (tagName, withDataAndEvents, deepWithDataAndEvents) { | |
// Use map to reconstruct the selector with newly created elements | |
return this.map(function() { | |
return jQuery.replaceTag(this, tagName, withDataAndEvents, deepWithDataAndEvents) | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment