Matching a value to JSON array values in jquery

Posted by chrism on Stack Overflow See other posts from Stack Overflow or by chrism
Published on 2010-05-14T14:04:54Z Indexed on 2010/05/14 14:14 UTC
Read the original article Hit count: 375

Filed under:
|

I have a JSON array that looks like this:

['Monkey','Cheetah','Elephant','Lizard','Spider']

I also have a text input. I want to test whether the value of the input upon 'blur' is also in the array and if it is do something.

Knowing a bit of python I tried something like this:

var existing_animals = ['Monkey','Cheetah','Elephant','Lizard','Spider']
$('input').blur(function() {
  user_animal = $(this).val()
  if (user_animal in existing_animals) {
    alert('Animal already exists!')
  }
});

So, how rookie is my mistake?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about jQuery