icecub
icecub

Reputation: 8773

Dynamicly create unicode variable from string

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?

Upvotes: 0

Views: 63

Answers (1)

Daniel A. White
Daniel A. White

Reputation: 190976

I think you want String.fromCodePoint

Upvotes: 1

Related Questions