Skip to content

Instantly share code, notes, and snippets.

@wkw
Last active May 30, 2020 05:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkw/7495913 to your computer and use it in GitHub Desktop.
Save wkw/7495913 to your computer and use it in GitHub Desktop.
jQuery Change Element Type usage: $("span").changeElementType("div")
// from Andrew Whitaker and Jazzbo, http://stackoverflow.com/a/15554920/161625
$.fn.changeElementType = function(newType) {
var newElements = [];
$(this).each(function() {
var attrs = {};
$.each(this.attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
var newElement = $("<" + newType + "/>", attrs).append($(this).contents());
$(this).replaceWith(newElement);
newElements.push(newElement);
});
return $(newElements);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment