How to use special characters like accented characters in JS?

1.2k views Asked by At

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.

1

There are 1 answers

2
bfavaretto On BEST ANSWER

Just use the actual character:

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