Hristo Hristo - 2 years ago 103
Javascript Question

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

Say I have an element like this...

<math xmlns="">
<mo class="symbol">α</mo>

Is there a way to get the unicode/hex value of alpha
, using JavaScript/jQuery? Something like...

$('.symbol').text().unicode(); // I know unicode() doesn't exist
$('.symbol').text().hex(); // I know hex() doesn't exist

I need
instead of
and it seems like anytime I insert
into the DOM and try to retrieve it right away, it gets rendered and I can't get
back; I just get α.

Answer Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download