You have to escape special characters using JavaScript syntax, not HTML syntax.
$('#password').attr('placeholder', 'Contrase\u00f1a');
Personally I'd use .prop()
instead of .attr()
too, though both should work here.
The general procedure is to
- Find the numeric equivalent for the HTML escape (for example,
ñ
can also be written ñ
);
- Convert the decimal numeric representation to hex (so,
241
-> F1
);
- Write a JavaScript
\uNNNN
escape with the 4-digit version of the hex value.
edit — "CBroe" makes the good point that if you maintain your source as a UTF-8 file, then you can just directly code that character and it should work:
$('#password').attr('placeholder', 'Contraseña');
For that to work requires that your server correctly sends out headers with the appropriate character encoding value, but you really should do that anyway. The hex escape will work even if you are correctly storing and serving UTF-8 of course.