15

I have the following JQuery code:

$(this).text('Options ▴');

However, the ascii code isn't showing up on my web page. All that shows up is on the webpage is Options ▴.

What am I doing wrong?

JGreig
  • 7,329
  • 5
  • 20
  • 11

5 Answers5

40

Use the .html() method instead of .text().

The whole point of .text() method is to make the text appear exactly as it's in the string, with all tags and entities.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
3

If you use a unicode escape ('\u25B2') instead of the html escape character, you can use the .text method.

Some users will not have a font that will display either version.

kennebec
  • 102,654
  • 32
  • 106
  • 127
  • Yes it's a downside that `.text('\u25B2')` may be less supported than `.html('▲')`. But one upside is that `.text()` is [safer](https://stackoverflow.com/a/49396917/673991) than `.html()` if there's any chance the string could ever one day come from untrusted sources (e.g. user input). – Bob Stein Jun 20 '19 at 15:53
2

&#x25B4 is definitely not ascii (ascii's a character code that runs from 0 to 127...!-) -- it seems to be a high-page unicode character. What char encoding is your page using? With something "universal" like utf-8, unicode characters should show up... if the browser has them in its font, of course. With other encodings, such characters might just be impossible to transmit and show.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • It's an arrow triangle. The crazy thing is that if I do within HTML "Options ▴", the unicode character displays. But if I do my code via JavaScript, the unicode does NOT display – JGreig Apr 28 '10 at 05:08
2

..or if someone wants to use just javascript:

var d = document.createElement("div");
d.appendChild(document.createTextNode("500"));
var euro = document.createElement("span");
euro.innerHTML = "€"; //your character
d.appendChild(euro);

prints: 500€

user791341
  • 21
  • 1
0

You can try this:

$(this).html('Options ▴');

Output:

Options ▴

If you want to test it online you can check out this website. (I liked it very much)

Krunal
  • 3,443
  • 3
  • 23
  • 27