Skip to content

Instantly share code, notes, and snippets.

@jmblog
Created August 1, 2012 02:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmblog/3222899 to your computer and use it in GitHub Desktop.
Save jmblog/3222899 to your computer and use it in GitHub Desktop.
Simple HTML encoding/decoding using jQuery
// http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding
function htmlEncode(value){
return $('<div/>').text(value).html();
}
function htmlDecode(value){
return $('<div/>').html(value).text();
}
@Lukenickerson
Copy link

A more reliable method is to use .replace to replace the problematic characters, as shown in this example and used by Angular. See this article for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment