loading
Please wait while loading...
Back Be Careful on using $.inArray

Today I'm find that the slide effect on my slide show plugin was fail to work. I used over half an hour to find the error reason and lastly found that was caused by $.inArray.

I check an variable using $.inArray() as shown below

if($.inArray('slide',['slide','e2','e3'])) {
    function();
}
 

function is expected to run if effect='slide', however, it didn't work at all.

Finally, I found that $.inArray() is different to the in_array() function in PHP, it is more consistant to the function array_search().
It will return the key(index) of the searching text if it is found in the array, otherwise it will return -1. i.e. if the searching text is get from the first element in the array, the return is 0 which is equal to false in js.

So, to check a string is inside an array of not, we should use equal to -1 to get a correct result.

effect = 'slide'
if($.inArray(effect,['slide','e2','e3'])!=-1) {
    function();
}
Comments
comments powered by Disqus