Skip to content

Instantly share code, notes, and snippets.

@etienned
Created June 15, 2012 03:30
Show Gist options
  • Save etienned/2934516 to your computer and use it in GitHub Desktop.
Save etienned/2934516 to your computer and use it in GitHub Desktop.
jQuery plugin that change element type, preserving attributes.
(function($) {
$.fn.changeElementType = function(newType) {
this.each(function() {
var attrs = {};
$.each(this.attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
$(this).replaceWith(function() {
return $("<" + newType + "/>", attrs).append($(this).contents());
});
});
};
})(jQuery);
@FireController1847
Copy link

Awesome. Works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment