Goal
Change content of a div into HTML encoded content for use in Ace editor.
Attempted Solution
I am trying to follow the solution here:
https://stackoverflow.com/a/1219983/1063287
With:
HTML
<div id="encode_me"><html>test</html></div>
jQuery
my_value = $("#encode_me");
$('<div/>').text(my_value).html();
jsFiddle
http://jsfiddle.net/rwone/3bFKG/8/
It is just outputting the text test
.
Edit: Alternatively, there is a newer answer on that page that suggests:
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
And I tried to implement that in a jsFiddle with the same results as the first attempt:
HTML
<div id="encode_me"><html>test</html></div>
jQuery
my_value = $("#encode_me");
my_value.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
jsFiddle