3

i am trying to display a pound sign in my html page.i want to display it through a variable because i am getting the values of sign from an xml file.

Here is Code:

var sign = $('#currencySign').text();
$('#monthly_Amt').text("\'"+sign+"\'"+monthlypayment);
<div id="monthly_amt"></div>
<div id="currencySign">\u00A3</div>

and the out put is

'\u00A3'450.33

and i want it as £450.33

Fiddle

How can I fix this?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
user2142786
  • 1,484
  • 9
  • 41
  • 74

7 Answers7

5

You can use \u00A3 ...

Demo

Alternatively you can use entity name as &pound; or entity number as &#163; as well but you need to use .html() and NOT .text()

And use var and not Var


As you commented, I see you are getting more troubles with this, if you want you can accomplish this easily with CSS like

#monthly_amt:before {
    content: "'£'"; /* Or you can use \00a3 instead of £ */
}

And your jQuery will be

var monthlypayment = 1000;
$('#monthly_amt').text(monthlypayment);

And if the element is dynamically generated, you can get rid of it using .remove()

Demo 2

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • Not sure why you struck out `.text()` ... it now reads: you need to use `.html()` and not not `.text()` =p – Ja͢ck Jun 12 '14 at 05:44
  • @Jack It's like `===` operator :P strict one haha – Mr. Alien Jun 12 '14 at 05:45
  • In any case, you don't need `.html()`. – Ja͢ck Jun 12 '14 at 05:46
  • @Jack weird, I used that cuz that didn't worked for me using `.text()` – Mr. Alien Jun 12 '14 at 05:48
  • Could you check [this jsbin](http://jsbin.com/qumorele/1/)? I hope it's not a browser thing :) – Ja͢ck Jun 12 '14 at 05:49
  • @Jack ok. I got where I confused myself, I used entity with `.text()` which failed... ;) and I edited it out, thanks – Mr. Alien Jun 12 '14 at 05:50
  • Actually, I ran OP's code and it [works fine](http://jsfiddle.net/VT2J2/2/) too, so I'm cv'ing it =/ – Ja͢ck Jun 12 '14 at 05:54
  • http://jsfiddle.net/coolsohi/kMFLG/ look into this example where i am wrong i am still getting same output – user2142786 Jun 12 '14 at 06:03
  • If the field is empty it would always have a pound symbol prefixed; not sure whether that's desirable; also, there's no reason to use `.html()`. – Ja͢ck Jun 12 '14 at 06:42
  • @Jack oops, I left it as is, will update the `.html` part and yea he can use `.addClass()` to the elements where the pound is required – Mr. Alien Jun 12 '14 at 06:44
  • Please don't use `` tags to decorate your links. The `` element is used to denote user input, not hyperlinks. – James Donnelly Jun 13 '14 at 07:51
  • @JamesDonnelly Its just ok to do so, and please take care while you edit, you just removed the demo 2 link – Mr. Alien Jun 13 '14 at 07:57
  • @Mr.Alien it *isn't* okay to do so as it goes against the HTML specification. Please see http://meta.stackexchange.com/a/181795/211113. Also Demo 2 wasn't (and still isn't) linked properly anyway, I only removed the `` tags. – James Donnelly Jun 13 '14 at 07:59
3

It is:

&pound; 

or

&#163;

You can check other encodings here:

http://www.w3schools.com/html/html_entities.asp

And here is a demo on how to do it: Online Demo

var sign = "&pound;";
$('#demo').html(sign+124.5);
Dalorzo
  • 19,834
  • 7
  • 55
  • 102
  • This will still render the entities as pure text, the OP is using correct thing but he requires `.html()` because `.text()` will fail... – Mr. Alien Jun 12 '14 at 05:29
1

To print a pound symbol, simply ... print the pound symbol:

var sign = '£';

$('#monthly_Amt').text(sign + monthlypayment);

Or, if that's somehow uncomfortable:

var sign = "\u00A3";

$('#monthly_Amt').text(sign + monthlypayment);  

Or, with the quotes:

$('#monthly_Amt').text("'" + sign + "'" + monthlypayment);  

Demo

Update

It seems that the "variable" actually comes from an HTML element, but \x00A3 only works in string literals.

This HTML fixes it:

<div id="currencySign">&pound;</div>

Demo

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

Try this one,

£   &pound; &#163;  &#xA3;  Pound Sterling

See this Link

     http://webdesign.about.com/od/localization/l/blhtmlcodes-cur.htm
KarthikManoharan
  • 805
  • 5
  • 12
0

Try using:

var sign = '&#163;'
$('#monthly_Amt').html("\'"+sign+"\'"+monthlypayment);

For the £ sign use: &#163; (ASCII Code)

http://jsfiddle.net/seckela/7gjF5/

EDIT: Using .text() will render the literal text, .html interprets it as an HTML element and will render special ASCII Characters using the ASCII codes.

0

Use &#163;

$(this).text('&amp;#163;');
  • If you try this and it does not work, just change the jQuery methods,

    $(this).html('&#163;');

This always work in all contexts...

Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
0

May be you have to look into it.

<div id="divResponse">
</div>

$(document).ready(function () {
var sign = "\u00A3"
var monthlypayment = "5000"
$('#divResponse').text(sign+monthlypayment);
});

here is the fiddle link : poundExampleJSFiddle

If this is helpful to you, please mark it as helpful.

Thanks

Arpit Jain
  • 455
  • 3
  • 8