Encode or Decode HTML entities with jQuery

Javascript fumction for client side HTML encode and decode

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

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

how its works…….

The div element is created bit it is never appended to the HTML document

use of .html(): Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

use of .text(): Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.

function htmlEncode: set the innerText of the element, and retrieve the encoded innerHTML
function htmlDecode: set the innerHTML value of the element and the innerText is retrieved.

Author : Rony T Sam

Advertisements

One thought on “Encode or Decode HTML entities with jQuery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s