Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created June 24, 2014 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save colelawrence/16878253f1bed1ded465 to your computer and use it in GitHub Desktop.
Save colelawrence/16878253f1bed1ded465 to your computer and use it in GitHub Desktop.
$.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