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?

+2


source to share


3 answers


Try the following:



var val = "John cars";
$('#subject input').filter(function() {
    return this.value == val;
});

      

+4


source


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.

+3


source


I'm pretty sure this is an apostrophe in the meaning of the search - try to elude it using

"John\ cars"

      

0


source







All Articles