Kousik
Kousik

Reputation: 22445

'.contains' is not working in android phones but its working fine in web page

I have a productlist and i want to scarch the productlist by input parameter. so I was using contains for scarching the input string in the productlist. its working fine with web page. but when i open the same page in mobile web page its not working. and giving an error that 'contains' in not define.

if(productlist[i].name.toLowerCase().contains(input_val.toLowerCase()))

    --my business logic--

after that i have tried with indexOf then its working fine in both cases.

if(productlist[i].name.toLowerCase().indexOf(input_val.toLowerCase()) !== -1)
    --my business logic --

what is the problem for .contains??

Upvotes: 4

Views: 286

Answers (1)

The Alpha
The Alpha

Reputation: 146219

Use this Polyfill, ( Reference : MDN)

if(!('contains' in String.prototype)) {
  String.prototype.contains = function(str, startIndex) { 
      return -1 !== String.prototype.indexOf.call(this, str, startIndex); 
  };
}

See the Compatibility table in Can I use...

Update : Also you can check this answer.

Upvotes: 2

Related Questions