Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: contains() works in Firefox but not Chrome/Safari

I've got an ajax function that gets some values based on the selected value in a dropdown. I'm trying to enable/disable some fields based on a substring. I found 'contains' to work in firefox, but testing in chrome and safari tells me that the object 'has no method contains'

if ($d.attr("embed").contains('$var_2$')) { 
    //do something
} else {
    // do something else
}

Is there an alternative to contains that will work in all browsers?

like image 397
richj Avatar asked Jul 03 '13 06:07

richj


1 Answers

what you're using there is String.contains, not jQuery.contains. this is because you're using the function on .attrs return-value, wich is a String, not a jQuery-Object.

to use jQuery.contains() (wich works cross-browser), you could do this instead:

$d.contains('$var_2$')

but note that this won't only search in the specified attribute but the whole element instead.

so what you most likely want to do is using String.indexOf() (wich also works cross-browser):

$d.attr("embed").indexOf('$var_2$') != -1
like image 83
oezi Avatar answered Sep 23 '22 02:09

oezi