Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape reserved words in Javascript

I have this piece of code in JavaScript (jQuery):

$("<a />").html("?").attr({
    href: "javascript:void();",
    class: "CorrentTooltip",
    title: "Explain!"
});

What are the alternatives to escape reserved word class?

ps.: The option class is causing a mess with my Javascript Parser in my IDE (Visual Studio) due to conflict with keyword class. So, the JS Parser says that there's a lot of syntax errors after this point. I can't collapse code blocks. It continues functional, but ugly.

like image 845
Andre Figueiredo Avatar asked Nov 26 '13 15:11

Andre Figueiredo


People also ask

How do you escape text in JavaScript?

Javascript uses '\' (backslash) in front as an escape character. To print quotes, using escape characters we have two options: For single quotes: \' (backslash followed by single quote) For double quotes: \” (backslash followed by double quotes)

What can I use instead of escape in JavaScript?

JavaScript escape() The escape() function is deprecated. Use encodeURI() or encodeURIComponent() instead.

How do you escape a special character in JavaScript?

To use a special character as a regular one, prepend it with a backslash: \. . That's also called “escaping a character”.


1 Answers

You can escape it by enclosing it using '' or ""

$("<a />").html("?").attr({
    href: "javascript:void();",
    'class': "CorrentTooltip",
    title: "Explain!"
});
like image 62
Arun P Johny Avatar answered Oct 21 '22 02:10

Arun P Johny