How do I get the unicode/hex representation of a symbol out of the HTML using JavaScript/jQuery? How do I get the unicode/hex representation of a symbol out of the HTML using JavaScript/jQuery? jquery jquery

How do I get the unicode/hex representation of a symbol out of the HTML using JavaScript/jQuery?


Using mostly plain JavaScript, you should be able to do:

function entityForSymbolInContainer(selector) {    var code = $(selector).text().charCodeAt(0);    var codeHex = code.toString(16).toUpperCase();    while (codeHex.length < 4) {        codeHex = "0" + codeHex;    }    return "&#x" + codeHex + ";";}

Here's an example: http://jsfiddle.net/btWur/


charCodeAt will get you the decimal value of the string:

"α".charCodeAt(0); //returns 9450x03b1 === 945; //returns true

toString will then get the hex string

(945).toString(16); // returns "3b1"

(Confirmed to work in IE9 and Chrome)


If you would try to convert Unicode character out of BMP (basic multilingual plane) in ways above - you are up for a nasty surprise. Characters out of BMP are encoded as multiple UTF16 values for example:

"🔒".length = 2 (one part for shackle one part for lock base :) )

so "🔒".charCodeAt(0) will give you 55357 which is only 'half' of number while "🔒".charCodeAt(1) will give you 56594 which is the other half.

To get char codes for those values you might wanna use use following string extension function

String.prototype.charCodeUTF32 = function(){       return ((((this.charCodeAt(0)-0xD800)*0x400) + (this.charCodeAt(1)-0xDC00) + 0x10000));};

you can also use it like this

"&#x"+("🔒".charCodeUTF32()).toString(16)+";"

to get html hex codes.

Hope this saves you some time.