6

From this question I learned that the check mark is the code ✔ (0x2714 [HTML decimal: ✔]). I know that you add text to a span using jQuery by doing $('#spanid').text(texthere);. I want to add the check mark to the span.

I did it like

$('#spanid').text(✔); //This errors on the `&`
$('#spanid').text(#10004); //This results in `Unexpected token ILLEGAL

What is the correct way of doing this?

Community
  • 1
  • 1
Pekka
  • 1,075
  • 2
  • 10
  • 17

4 Answers4

6

Use .html(). Also, enclose the value in quotes.

$('#spanid').html('✔');

.text() will convert the input to text string. .html() converts to HTML string/content and the character encoded can be seen.

Fiddle Demo

or if you already have the character , .text() would work;

$('#spanid').text('✔');
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
3

What I would do is:

$('#spanid').addClass('check');

and add css;

.check:after {
  content: '(what ever the code for the check mark is)';
}
Ram
  • 143,282
  • 16
  • 168
  • 197
Flo
  • 359
  • 1
  • 9
  • Interesting solution and technically still is a jQuery solution. – Chad May 02 '15 at 05:08
  • I like it more as you can remove is with jquery with .removeClass(''); Not that it is necessary in this situation. @Chad – Flo May 02 '15 at 05:12
  • I'm not a huge fan of using `content` in css but I must admit this is actually quite elegant compared to other uses I've seen. +1 – Chad May 02 '15 at 05:14
2

Alternatively, you could create checkmark with String.fromCharCode:

$('#spanid').text(String.fromCharCode(10004));
nderscore
  • 4,182
  • 22
  • 29
1

Try

$('#spanid').html ('✔');

instead of text(). The text function escapes the string.

Chad
  • 1,531
  • 3
  • 20
  • 46
al.scvorets
  • 122
  • 9