Last active
December 7, 2024 12:26
-
-
Save nurbek-ab/092f72cdb69ad7a3ee12 to your computer and use it in GitHub Desktop.
Replace element's tag name with another tag name using jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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