You can pass a function to .replaceWith
[docs]:
$('code').replaceWith(function(){
return $("<pre />", {html: $(this).html()});
});
Inside the function, this
refers to the currently processed code
element.
Update: There is no big performance difference, but in case the code
elements have other HTML children, appending the children instead of serializing them feels to be more correct:
$('code').replaceWith(function(){
return $("<pre />").append($(this).contents());
});