13

How do i display a string with Special Characters like € in a Javascript/jQuery alert?

eg: I want to display a message box with "The Price is €10"

But when i use the below code:

alert("The Price is €10");

The Output shown in the message box is "The Price is €10", I want my output to be "The Price is €10".

Can some help me with this please? Thanks in advance.

Chamara Keragala
  • 5,627
  • 10
  • 40
  • 58

3 Answers3

18

Use this as the alert. Works fine for me.

alert(' The Price is \u20AC 10');

The description is here : http://leftlogic.com/projects/entity-lookup/

Riju Mahna
  • 6,718
  • 12
  • 52
  • 91
10

The native alert method does not decode HTML encoded entities.

But browsers do while rendering HTML. One hack is to create a HTML element with the specific text as its innerHTML (so it does the character processing), then get back its text property and alerting it out.

function alertSpecial(msg) {
    msg = $('<span/>').html(msg).text();
    alert(msg);
}
alertSpecial('The Price is &euro;10');

This will work for all &xxx characters that the browser can display, without needing to find out the character code for each special character you may want to use.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • Nice! That did the trick for me. I was geting a json response with encoded html characters in a string from an API where i can't modify the output to UTF8. – Matej Svajger Apr 13 '16 at 13:51
  • An absolutely honourable response. This is the only workaround to properly show UTF-8 special characters and emojis in alert() functions retrieved from server-side AJAX responses sent as UTF-8. – andreszs Jun 02 '23 at 20:28
2

Check :

alert("The Price is \u20AC 10");

http://jsfiddle.net/Nseum/

Unicode Character 'EURO SIGN' (U+20AC)

hsuk
  • 6,770
  • 13
  • 50
  • 80