Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Client-side HTML Encode and Decode functions with jQuery

0.00/5 (No votes)
21 Mar 2011 1  
HTML encoding and decoding is used daily by web developers. ASP.NET provide us with build-in server-side functions to perform this job. This article

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

HTML encoding and decoding is used daily by web developers. ASP.NET provide us with build-in server-side functions to perform this job. This article will show you a simple implementation of client-side HTML encoding and decoding functions with the help of jQuery.

 

function htmlEncode(value) {
    return $('<div/>').text(value).html();
}
 
function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

 

Basically it creates a new invisible element, put HTML into it and take out as text or on the other way put text into it and take out as HTML.

It's simple but quite handy.

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here