Skip to content

Instantly share code, notes, and snippets.

@DDtMM
Forked from etienned/changeElementType.js
Last active December 19, 2015 22:49
Show Gist options
  • Save DDtMM/6029977 to your computer and use it in GitHub Desktop.
Save DDtMM/6029977 to your computer and use it in GitHub Desktop.
This version chains, returning a jquery object with updated children.
(function ($) {
$.fn.changeElementType = function (newType) {
var attrs, elem, $elem;
for (var i = 0, il = this.length; i < il; i++) {
attrs = {};
$elem = $(elem = this[i]);
$.each(elem.attributes, function (index, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
$elem.replaceWith($elem = $("<" + newType + "/>", attrs).append($elem.contents()));
this[i] = $elem[0];
}
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment