1

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

1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

I have html encoded values in variable like &gt &lt and &nbsp
Use replace() like alert(b.replace('&nbsp;',' '));
your solution is specific for space and br only . but there can be any html encoded character in message. I am looking for one solution that could could decode any html decoded tag.
@PraveenD the problem with using alert is that it doesn't support HTML, only plain text.
There is no way to get proper message in alert. If not there will be big change in my project.
0

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, i.e. 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);

4 Comments

If your message is => I&#39;ll \&quot;walk\&quot; the &lt;b&gt;dog&lt;/b&gt; now . then what will u do?
Can you edit as per my question. I have tryed this in alert not working.
It is showing <br> inside string it should show in new line.
@PraveenD Have a look at the edited 'Try it here - DEMO' jsfiddle link in my post. That works but it pants as you would have to replace all problem html tags with a javascript equivalent... look at using something like jqueryui.com/dialog/#modal-message instead - this will render most html as a web page would.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.