1

I'm trying to use JavaScript to set the value of an Input Text Box to this Emoji >>
But it didn't work as I expect it to.
I've tried several different format to express the Unicode but none of it works.
I've included the snippet that I've tried.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>

    <script>
      var myTextBox = document.createElement("input");
      document.body.appendChild( myTextBox );
      myTextBox.value = "&#129300";

      // None of below will work:
      // \u1F914
      // \xF0\x9F\xA4\x94
      // &#129300;
    </script>

    <p> &#129300; </p>

  </body>
</html>

Any idea on how to do this properly?

MrYellow
  • 113
  • 3
  • Check this [link](https://stackoverflow.com/questions/22312364/printing-emojis-with-javascript-and-html). You find find some solution for it – Raw Scripter Aug 27 '18 at 14:20

1 Answers1

2

You need to convert it into a surrogate pair: "\uD83E\uDD14"

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>

    <script>
      var myTextBox = document.createElement("input");
      document.body.appendChild( myTextBox );
      myTextBox.value = "\uD83E\uDD14";
    </script>

    <p> &#129300; </p>

  </body>
</html>
weirdan
  • 2,499
  • 23
  • 27