Skip to content

Instantly share code, notes, and snippets.

@premsh
Created December 10, 2013 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save premsh/7899645 to your computer and use it in GitHub Desktop.
Save premsh/7899645 to your computer and use it in GitHub Desktop.
Replace the html tag
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());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment