0

I have this code. I am using contains function to check if the value stored in the string or not but getting error. can anyone help me how to check this.

var lang = reply.LANGUAGES[InstObj.langId];
var instElement = reply.QUAL[iCount] ;

if(instElement.contains(lang))
{
    alert(lang);
}
else
{
    alert(instElement);
} 
Rahul Pandey
  • 435
  • 1
  • 3
  • 13
  • This might help http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring-in-javascript Use `indexOf` – Jason Evans Dec 20 '13 at 11:25
  • Interestingly there is a [`contains()` coming soon](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains), but currently few browsers support it. – Andy Dec 20 '13 at 11:26
  • 1
    Don't confuse this with the [`contains` method in jQuery](http://api.jquery.com/jQuery.contains/) tho. That's specifically a DOM method. – Andy Dec 20 '13 at 11:27
  • Use indexOf...http://www.w3schools.com/jsref/jsref_indexof.asp – aberry Dec 20 '13 at 11:28

1 Answers1

3

Use indexOf()

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex, returns -1 if the value is not found.

Change your code as

var lang = reply.LANGUAGES[InstObj.langId];
var instElement = reply.QUAL[iCount] ;

if(instElement.indexOf(lang) > -1)
{
    alert(lang);
}
else
{
    alert(instElement);
} 
Satpal
  • 132,252
  • 13
  • 159
  • 168