Skip to content

Instantly share code, notes, and snippets.

@evonsdesigns
Last active August 29, 2015 14:01
Show Gist options
  • Save evonsdesigns/3ccd7af3318243f01003 to your computer and use it in GitHub Desktop.
Save evonsdesigns/3ccd7af3318243f01003 to your computer and use it in GitHub Desktop.
HTML Encode Decode Textarea Javascript
<button type="button" class="btn btn-default btn-xs" onclick="htmlEncode()" title="encode">HTML encode</button>
<button type="button" class="btn btn-default btn-xs" onclick="htmlDecode()" title="encode">HTML decode</button>
<br/>
<textarea id="encodedecode" cols="80" rows="10"></textarea>
<script>
function htmlEncode(){
var encoded = $("#encodedecode").val().replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
$("#encodedecode").val(encoded);
}
function htmlDecode() {
var decoded = $("#encodedecode").val().replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '\"')
.replace(/&apos;/g, '\'');
$("#encodedecode").val(decoded);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment