Pages

Showing posts with label searching string in javascript. Show all posts
Showing posts with label searching string in javascript. Show all posts

Monday, April 2, 2012

indexOf is not working in javascript OR searching a item in a list OR searching a item in array in javascript

indexOf is not working in javascript.

//code :  // not wotking
var commonerr = [ 1, 2, 3, 4, 5 , 6 ];     //array              

if (commonerr.indexOf(errorNumber)  != -1){   
// to do 
}


IndexOf is not supported in Internet explorer. Its supported in FF and other browsers.

I found few blogs and its seems like they have a work around for this. You can simply use prototype to extend this method.  please refer below url.
http://www.pearweb.com/javascript/array-index-of.html

2nd way :

Or you can use below code. Jquery has a nice method "inArray" which will help you in searching a item in a array.

here is the link of jquery
http://api.jquery.com/jQuery.inArray/

var commonerr = [ 1, 2, 3, 4, 5 , 6 ];     //array                      
                            
if ( $.inArray(errorNumber, commonerr) != -1){  
//to do
}