Hey again (on a roll today).
In jQuery/Javascript is there a way of effectively having this:
var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
//get input from user
if (inputFromUser == anythingInArray) {
alert("it's possible!);
}
Hey again (on a roll today).
In jQuery/Javascript is there a way of effectively having this:
var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ];
//get input from user
if (inputFromUser == anythingInArray) {
alert("it's possible!);
}
You can use inArray:
var result = $.inArray(inputFromUser, myArray);
if (result >= 0)
{
alert('Result found at index ' + result);
}
jQuery: $.inArray()
if($.inArray('one', myArray) > -1) {}
If you need to do it without jQuery, you can use the Array.prototype.indexOf method like
if(myArray.indexOf('one')) {}
That is restricted to ECMAscript Edition 5. If a browser doesn't support that do it the classic route:
for(var i = 0, len = myArray.length; i < len; i++) {
if(myArray[i] === 'one') {}
}
Ref.: $.inArray()
look at the answer in :
http://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array