0

I have around 3000 html elements each representing a certain browser emoji. The elements contain a CSS class representing the emoji's unicode:

<span class="_1f603"></span>
<span class="_1f604"></span>

I want to convert the unicode to its representing short code:

\u{1f603} = ':grinning:'

I already have a working function for that part. The problem is building up the correct unicode string using the class name (as I don't intend to do all 3000 elements manually).

To get what I want I've written the following code:

function setShortCode(ele){
    let classArray = ele.className.split(' ');

    for(let i=0; i<classArray.length; i++){
        if(classArray[i].startsWith('_')){
            htmlUnicodeString = classArray[i].substr(1);
            jsUnicodeString = '\u{'+htmlUnicodeString+'}';

            document.getElementById('chat-text').value = document.getElementById('chat-text').value + joypixels.toShort(jsUnicodeString);
        }
    }
}

It returns this error:

SyntaxError: malformed Unicode character escape sequence

Appearently it's not allowed to dynamicly create unicode strings in this way?

2
  • 1
    String.fromCodePoint Commented Oct 4, 2019 at 0:38
  • @DanielA.White Amazing! Thanks a lot, that worked like a charm! Feel free to turn it into an answer if you want :) Commented Oct 4, 2019 at 0:44

1 Answer 1

1

I think you want String.fromCodePoint

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.