Skip to content

Instantly share code, notes, and snippets.

@js1972
Created September 5, 2013 04:00
Show Gist options
  • Save js1972/6445942 to your computer and use it in GitHub Desktop.
Save js1972/6445942 to your computer and use it in GitHub Desktop.
#JavaScript for #html encoding and decoding a value with #jQuery
/*
Use jQuery to html encode a value
*/
function htmlEncode(value){
//create a in-memory div, set its inner text(which jQuery automatically encodes)
//then grab the encoded contents back out. The div never exists on the page.
return $('<div/>').text(value).html();
}
/*
Use jQuery to html decode a vlaue
*/
function htmlDecode(value){
return $('<div/>').html(value).text();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment