Skip to content

Instantly share code, notes, and snippets.

@MaraScott
Last active May 19, 2020 18:26
Show Gist options
  • Save MaraScott/4571523 to your computer and use it in GitHub Desktop.
Save MaraScott/4571523 to your computer and use it in GitHub Desktop.
Name : htmlEncode("text"), htmlDecode("text") - Language : JavaScript, jQuery - type : function - platform : generic
// http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding?answertab=votes#tab-top
function htmlEncode(value){
"use strict";
//create a in-memory div, set it's 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();
}
function htmlDecode(value){
"use strict";
return $('<div/>').html(value).text();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment