Raghava Attuluri
Raghava Attuluri

Reputation: 31

innerhtml is ignoring HTML escaped characters

I have a div like

<div id="first"></div> 

and I try to change content (which I get from backend html escaped) as

$( "#first" ).html( "Some text <div style=&#34;font-size:150%&#34;>This text should look bigger</div>" );

----------OR------------

$( "#first" ).html( "Some text <div style=&quot;font-size:150%&quot;>This text should look bigger</div>" );

The output in browser is

<div id="first">Some text <div style="&quot;font-size:150%&quot;">This text should look bigger</div></div>

This is not applying the style I wanted to apply as in the DOM I see two times " (quote). I also tried the classic way of document.getElementById with out any success.

Thank's in advance for any idea.

Upvotes: 0

Views: 395

Answers (1)

Jack
Jack

Reputation: 2830

You can decode escaped text with this function:

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

You should pass escaped HTML from the backend through this function before writing it to the page.

Upvotes: 1

Related Questions