Skip to content

Instantly share code, notes, and snippets.

@mephraums
Last active December 25, 2015 01:19
Show Gist options
  • Save mephraums/6893666 to your computer and use it in GitHub Desktop.
Save mephraums/6893666 to your computer and use it in GitHub Desktop.
jQuery Change Element Type (jQuery Plugin)
//Change Element Type plugin
//Source: http://stackoverflow.com/questions/8584098/how-to-change-an-element-type-using-jquery
$.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);
};
//Usage: $('input[type="text"]').changeElementType('date');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment