How to get Raw html from string in jquery?
Asked Answered
P

2

11

I have <label class='ash'>Comment Removed</label> in the database.

When I show this on the grid. I get this on the page:

<label class='ash'>Removed</label>

Actually I should just get Removed in Gray color

How can I convert this to Html like I do in MVC 3 Razor view?

@Html.Raw(HttpUtility.HtmlDecode(comment.txt)) works fine

I am using jquery 1.6 on MVC 3

I tried:

$("<label class='ash'>Comment Removed</label>").html()
   unescape($(txt)).html()

May be it is simple, but can't figure it out

Preceptor answered 13/5, 2011 at 22:38 Comment(1)
This solution might helpEssie
A
11

This should do the trick for you:

var elemString = $('<div/>').html("&lt;label class='ash'&gt;Comment Removed&lt;/label&gt;").text();

Here's a demo showing it being appended to the body ->

If you need to do this multiple times, you could simplify with a function, like so:

function DecodeHtml(str) {
    return $('<div/>').html(str).text();
}

var encodedStr = "&lt;label class='ash'&gt;Comment Removed&lt;/label&gt;";
$('body').append(DecodeHtml(encodedStr));
Alagez answered 13/5, 2011 at 22:48 Comment(0)
N
1

jQuery :

var YuorHtml = "<p>Some Text  <em>Some Text</em> <strong>Some Text</strong></p>";
  
 $('#YuorID').html(YuorHtml)
Novick answered 4/5, 2021 at 2:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.