Peter White
Peter White

Reputation: 1086

Why can't I get this entity code to display correctly in a browser?

I'm trying to code a UK Pound symbol to be written to a document by JavaScript, but it's code is not being translated and is instead displayed as entered.

See this JSBin http://jsbin.com/orocox/1/edit

This is the JavaScript:

$("#price").text('£ 1.99');

This is the html:

<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
  <meta charset=utf-8 />
  <title>JS Bin</title>
</head>
<body>
  <span id="price"></span>
</body>
</html>

This is the result: '&pound(;) 1.99'

*Note that the parenthesis around the ';' are added by me to prevent the entity code from being translated on StackOverflow, but they do not exist in the actual output.

The result I want is: '£ 1.99'.

Upvotes: 0

Views: 170

Answers (3)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201518

Use the character itself:

$("#price").text('£ 1.99');

This is good for the readability of your code. How you type “£” depends on your editing environment. E.g., on Windows, you can produce it by typing Alt 0163 if you cannot find any more convenient way (depending on keyboard, keyboard layout, and editor being used).

Upvotes: 0

CME64
CME64

Reputation: 1672

use unicode instead: jsbin

$("#price").text('\u00A3 1.99');

explanation: the &pound; is an html entity and is not processed as normal text. but unicode works for any text. since you are using text it is processed as a string not an html.

check this page's encoding reference : here

Upvotes: 4

andre.barata
andre.barata

Reputation: 663

Try $("#price").html('&pound; 1.99'); instead.

Upvotes: 2

Related Questions