Reputation: 1383
I need to pass a string with dot('.') as a part of the jquery selector. Even after escaping the dot by adding '\\' none of the jquery functions are working as expected.Following is the code:
var id = "www.google.com";
var vgid = id.replace(/\./g, '\\\\.'); //adding '\\' before dot to escape it
var flagged = $('#flagged_'+vgid).val(); // retrieving the value of a field NOT working
The above operation is not returning the value of the field. However, if I hardcode the value with the escape characters its giving me the value.
var flagged = $('#flagged_www\\.google\\.com').val(); //this is working
Upvotes: 0
Views: 165
Reputation: 1816
By the looks of it jQuery needs the values to be double escaped, which is what you are doing, ie.
$(".a\\.b")
The only applies when you are entering the value as a string (Since this is escaped before it does the css lookup). Since you are adding the value directly, it seems the /
only need to be escaped once meaning is what you'd be after is:
var escaped = "www.google.com".replace(/\./g, '\\.');
console.log($('#flagged_'+ escaped).val());
See: http://jsfiddle.net/vjrL3og9/2/
Upvotes: 0
Reputation: 943769
You need to have one slash before each dot in the selector so that the dot is escaped for CSS
In your hard coded example you need two, because to write the CSS escape character in a JS string literal, you must escape the slash character for the JS parser.
You are putting two in the generated example but should have only one.
var vgid = id.replace(/\./g, '\\.');
Upvotes: 5