Saturday, December 7, 2013
This article provides a sample implementation in jQuery to escape HTML characters suitable for pasting into text area inputs. Escape of HTML characters can be easily achieved with jQuery. jQuery support the .html() function to get HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

Using jQuery the script to escape HTML characters is listed below.

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    var input_text = $("#ip").val();
    $("#op").val(htmlEncode(input_text));
  });
});
function htmlEncode(value){ 
    if (value) {
        return $('<div/>').text(value).html(); 
    } else {
        return '';
    }
}
</script>
</head>

To do the reverse (un-escaping) using jQuery the script to escape HTML characters is listed below.

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    var input_text = $("#ip").val();
    $("#op").val(htmlDecode(input_text));
  });
});
function htmlDecode(value){ 
    if (value) {
        return $('<div/>').html(value).text(); 
    } else {
        return '';
    }
}
</script>
</head>

1 comment :

  1. it's very nice, but i(us) need sample output to enjoy the understanding, can you gave me (us)please.

    ReplyDelete

Contact Form

Name

Email *

Message *

Back to Top