Skip to content

Instantly share code, notes, and snippets.

@blubbll
Created September 3, 2018 15:02
Show Gist options
  • Save blubbll/53ae73eb04bfdf54e9f899a360317e09 to your computer and use it in GitHub Desktop.
Save blubbll/53ae73eb04bfdf54e9f899a360317e09 to your computer and use it in GitHub Desktop.
jQuery replaceWith
//HTML-Tag mit jQuery ändern
$.extend({
replaceTag: function (element, tagName, withDataAndEvents, deepWithDataAndEvents) {
var newTag = $("<" + tagName + ">")[0];
$.each(element.attributes, function () {
newTag.setAttribute(this.name, this.value);
});
$(element).contents().clone(withDataAndEvents, deepWithDataAndEvents).appendTo(newTag);
return newTag;
}
})
$.fn.extend({
replaceTag: function (tagName, withDataAndEvents, deepWithDataAndEvents) {
// Use map to reconstruct the selector with newly created elements
return this.map(function () {
return jQuery.replaceTag(this, tagName, withDataAndEvents, deepWithDataAndEvents);
})
}
})
$(this).replaceWith($(this).clone().replaceTag("a").attr('href', $(this).data('href'))); //Element zu link machen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment