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!);
}
is it this?
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()