indexOf is not a function in Firefox, Opera but works in IE, indexOf alternative in javascript to test string contains?

user840930 picture user840930 · Jun 12, 2012 · Viewed 37.3k times · Source

Getting an error using indexOf call in Javascript on Firefox and Opera. Works fine in IE.

Following is the error message:

Action

function anonymous(Grid, Row, Col, Event) { 
    return Grid.ActionShowPopupMenu(); 
} 

for event OnRightClick failed with exception: row.id.indexOf is not a function

I'm testing that a string contains another string in Javascript and using the indexOf function of a string. The calls however are being made in JQuery functions. Perhaps that is the reason for the problem? Is there an alternative to using indexOf in Javascript to test if a string contains another string? Is there a workaround for this problem?

Answer

RomainValeri picture RomainValeri · Jun 12, 2012

String.indexOf is perfectly OK in all browsers. I assume the id property of your row object is no string (nor array, btw, because indexOf is also defined on arrays (except for IE))