JQuery selector issue in Internet Explorer
I have a jquery selector that looks like this:
var val = "John cars";
$('#subject input[value="'+val+'"]')
This works fine in Firefox, Chrome, Internet Explorer 8, but not IE 6 or IE7. The problem is "finding text". Does anyone know how to get around this problem other than looping through all nested data and comparing strings?
source to share
Try it '
with a backslash
var val = "John\ cars";
$('#subject input[value="'+val+'"]')
from jQuery
Special characters in selectors
If you want to use any of the metacharacters described above as the literal part of a name, you must avoid the backslash character (). Since Javascript uses backslashes for escape sequences in string literals, you must use two backslashes (\) in string literals, so that one backslash will be placed in the string.
Example:
"#foo\\:bar"
"#foo\\[bar\\]"
"#foo\\.bar"
A complete list of characters required for escaping:
#;&,.+*~':"!^$[]()=>|/
EDIT:
since the value is in a string literal, it must be double escaped. So this works
var val = "John\\ cars";
$('#subject input[value="'+val+'"]')
Thanks bobince for pointing it out.
source to share