简体   繁体   中英

javascript is not decoding html values

I am trying to achieve at below

var a = "how are you. <br> fine";
var b = "how are you, &lt;br&gt; fine";

alert(a);
alert(b);

Output should be

How are you 
fine 

But It gives me

how are you. <br> fine
how are you, &lt;br&gt; fine

&gt &lt and &nbsp are in my string

How can I decode this html in alert message of javascript

Ty to use \\n in place of <br> like,

var b = "how are you, \n fine";
alert(b);

Live Demo

To show HTML content you can try jquery-alert

Try this...

var text = '&lt;p&gt;name&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:xx-small;"&gt;ajde&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;da&lt;/em&gt;&lt;/p&gt;';
var decoded = $('<div/>').html(text).text();

alert(decoded);

Edit: As Rohan Kumar has suggested above, I don't think it is possible to render HTML inside an alert box (which is what you are implying in your question). but rather you would need to do something like below, ie use replace or even better look into using a modal dialogue

var text = 'how are you, &lt;br&gt; fine';
var text = text.replace('&lt;br&gt;','\n');
var decoded = $('<div/>').html(text).text();

Try it here - DEMO

alert(decoded);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM