richj
richj

Reputation: 461

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?

Upvotes: 10

Views: 8989

Answers (3)

oezi
oezi

Reputation: 51797

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

Upvotes: 18

Ilu
Ilu

Reputation: 904

You can use indexOf, it should work in all browsers.

if ($d.attr("embed").indexOf('$var_2$') > -1) { 
    //do something
} else {
    // do something else
}

Upvotes: 3

SimaWB
SimaWB

Reputation: 9294

You can use indexOf instead of contains.

Upvotes: 0

Related Questions