Replace item and save attributes

The following almost work to replace all instances of span[data-type="yesno"] with lis, but I would also like to keep attributes, classes, etc. Is there a way to transfer attributes in the same way as html?

 $('span[data-type="yesno"]').replaceWith(function(){ return $("<li>", {html: $(this).html()}); }) 
+6
source share
1 answer

You need to loop through the attributes of the element:

 $('span[data-type="yesno"]').replaceWith(function(){ $li = $("<li>", {html: $(this).html()}); $.each(this.attributes, function(i, attribute){ $li.attr(attribute.name, attribute.value); }); return $li; }) 
+10
source

Source: https://habr.com/ru/post/959258/


All Articles