Decode html encoded string using Jquery


Lets say you have encoded html string in database or in some string object which looks like – 
<ul><li>sdfsd</li><li>sdf</li><li>sdfsss</li></ul>

Now, when you want to display that as a Unordered list (i.e bulletted text list) using jquery’s html() method, you need to decode that string first to html string(i.e <ul><li>sdfsd</li><li>sdf</li><li>sdfsss</li></ul>) and then pass that html string to html() of jq.

To do that.

var decodedStr = ($(“<div/>”).html(encodedStr).text());
$(“#yourNewDiv”).html(decodedStr);

 

Advertisements

2 thoughts on “Decode html encoded string using 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