Getting HTML entities into document.createTextNode

1k views Asked by At

Suppose I have a string created like this: str = '\\' + 'u00eb'. If I do document.createTextNode(str), it will give me '\u00eb', instead of ë. Dont't ask me why, but I can't define my string as str = '\u00eb'. Defining my string as str = 'ë' or 'ë' doesn't help me either. Any suggestions on how to get 'ë' printed?

2

There are 2 answers

2
Pointy On

How about

var str = String.fromCharCode(0xeb);
0
jfriend00 On

If the data coming in looks like this:

u00eb

then, you can parse the hex string yourself:

function insertText(parent, str) {
    if (str.charAt(0) == 'u') {
        str = String.fromCharCode(parseInt(str.slice(1), 16));
    }
    parent.appendChild(document.createTextNode(str));
}

Working demo: http://jsfiddle.net/jfriend00/pk5Bp/