javascripthtmlcharacter-entities

How to use special characters like accented characters in JS?


In HTML, there are character entities to input any type of character. In javascript, they don't work in strings.

I want to compare a string with an elements value with an accent (written in Spanish):

HTML element:

<h1>Informaci&oacute;n</h1>

JS comparing to string:

if(document.getElementById("name").innerHTML == "Informaci&oacute;n")
    // ..

The JavaScript doesn't think the two elements are equal. I'm pretty sure I cannot type in a special character like an o with acute in Notepad++ and then escape it in JavaScript.


Solution

  • Just use the actual character:

    if(document.getElementById("name").innerHTML == "Informació") // is that Catalan?
        // ..