Skip to content

Instantly share code, notes, and snippets.

@nurbek-ab
Last active June 19, 2023 07:43
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nurbek-ab/092f72cdb69ad7a3ee12 to your computer and use it in GitHub Desktop.
Save nurbek-ab/092f72cdb69ad7a3ee12 to your computer and use it in GitHub Desktop.
Replace element's tag name with another tag name using jQuery
function replaceElementTag(targetSelector, newTagString) {
$(targetSelector).each(function(){
var newElem = $(newTagString, {html: $(this).html()});
$.each(this.attributes, function() {
newElem.attr(this.name, this.value);
});
$(this).replaceWith(newElem);
});
}
replaceElementTag('span', '<div></div>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment