Alerting Special Characters using jQuery/JavaScript

14,193

Solution 1

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/

Solution 2

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.

Solution 3

Check :

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

http://jsfiddle.net/Nseum/

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

Share:
14,193
Chamara Keragala
Author by

Chamara Keragala

Sysadmin/Developer

Updated on June 07, 2022

Comments

  • Chamara Keragala
    Chamara Keragala almost 2 years

    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 &euro;10");
    

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

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

  • Chamara Keragala
    Chamara Keragala over 11 years
    What is the UTF8 for character å
  • Riju Mahna
    Riju Mahna over 11 years
    Try alert(' The Price is \u00E5 10'); And please have a look at the link I gave in my answer. just type 'a' and it will show all the related characters
  • Chamara Keragala
    Chamara Keragala over 11 years
    Thanks alot i found it - alert(' The Price is \u00E5 10');
  • Matej Svajger
    Matej Svajger about 8 years
    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.
  • Heitor
    Heitor over 6 years
    Broken link :( d
  • CoqPwner
    CoqPwner almost 3 years
    The link is indeed broken, try this table as reference instead utf8-chartable.de. Simply replace the U+0008 by \u0008 or U+00A9 by \u00A9, for example.