3

I am returning special characters (specifically °) in JavaScript/jQuery, but it is not converting the entity to the desired character in the display. How can I display the degree sign correctly?

It must be simple; what am I missing? I have checked decodeURL, but it does nothing.

Demo fiddle

The HTML:

<p>Try to give answer is: </p>
<div id="target"></div>

<p>But should look like:</p>
<div> 5 &deg;C</div>

And the Javascript with jQuery:

$('#target').text('5 &deg;C');

Output:

Output from the fiddle

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
Sablefoste
  • 4,032
  • 3
  • 37
  • 58
  • possible duplicate of [Unescape HTML entities in Javascript?](http://stackoverflow.com/questions/1912501/unescape-html-entities-in-javascript) –  May 01 '15 at 16:15

2 Answers2

8

To see the interpreted character entity you need to use html():

$('#target').html('5 &deg;C');

Updated fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Just a quick note for future reference, this pointed out the nature of my error, but in the end, I used `append` not `html`, because that is what I am actually doing. – Sablefoste May 01 '15 at 16:19
  • `append()` would work to as it does not escape any characters. I think `text()` is the only jQuery method which has that behaviour. – Rory McCrossan May 01 '15 at 16:20
0

Try this:

$('#target').html('5 &deg;C');

text function escapes the html characters so you get what you see.

al.scvorets
  • 122
  • 9