How to avoid a single quote in jQuery?

I am trying to use the escape function to escape from a single quote:

var tagDesc = "Workers'_Compensation"; tagDesc = escape(tagDesc); $("#" + tagDesc + ".tag").css("display", "none"); 

The escape function replaces the single quote %27 with "Workers%27_Compensation" .

So, I get an error message,

Microsoft JScript runtime error: syntax error, unrecognized expression: # Workers% 27_Compensation.tag

+4
source share
2 answers

Use backslash

"Workers\'_Compensation";

Inside the selector, you would require 2 of them "Workers\\'_Compensation";

Check script

+9
source

jQuery is JavaScript and you can use a backslash to exit a special character.

C \ you can escape '
Try the following:

 var tagDesc = "Workers\\'_Compensation"; tagDesc = escape(tagDesc); $("#" + tagDesc + ".tag").css("display", "none"); 
0
source

Source: https://habr.com/ru/post/1483670/


All Articles