3

I have the following context where I want to turn a pattern into an emoji, however what I see as a result is &#1F600;. The code &#1F600; does not render into an emoji.

Any idea why?

$('#m').keyup(function (){ value = $('#m').val().replace(/(\:\))/g, '&#1F600;'); $('#m').val(value); }); 

I have also tried with U+1F600 and has the same result.

1
  • both .val-s ? Commented Mar 31, 2018 at 14:44

1 Answer 1

9

You're missing the 'x' for a hex entity value, 😀

😀

For passing this emoji in a JavaScript string, as the character itself and not as an HTML entity, you need two characters -- JavaScript strings can only do 16-bit codepoints directly, so Unicode codepoints beyond \uFFFF have to be broken up into two 16-bit values, like this:

'\uD83D\uDE00' 

These are called "surrogate pairs".

Alternatively, you can create an expression for the emoji character this way too:

String.fromCodePoint(0x1F600) 

You can even use the emoji characters directly in your code, so long as you're saving your code in a compatible encoding, like UTF-8, and you can count on the code editors you'll be using to handle an appropriately extended character set.

Sign up to request clarification or add additional context in comments.

15 Comments

Maybe it's browser dependent? It worked fine for me using Firefox.
I just opened this page in Chrome, and the emoji (a smiley) appears just fine in Chrome too.
I've added more to my answer above that might help.
based on what did you broken it down ? cause I need more emojis to convert ? :)
I set a string to the smiley in the Chrome JavaScript console like this: let a = '😀';, then I got the values for a.charCodeAt(0).toString(16) and a.charCodeAt(1).toString(16).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.