In
this post, I’ll show you how you can encode or decode (if you ever need to do
so) supplied HTML using jQuery.
This
is quite easy just create two separate method for encoding and decoding as
given below.
function encodeHTML(text)
{
if (text) {
return jQuery('<div />').text(text).html();
} else {
return '';
}
}
function decodeHTML(text)
{
if (text) {
return $('<div />').html(text).text();
} else {
return '';
}
}
|
Test
the both method to encode or decode HTML.
$(function () {
//pass the html to encode.
alert(encodeHTML('You & me are good friends'));
//pass the html to decode.
alert(decodeHTML('You & me are good
friends'));
});
|
Check working sample and code
|
Comments
Post a Comment