Skip to content Skip to sidebar Skip to footer

Javascript/jquery Compare Input Value To Array

I'm relatively new to javascript and jquery. Right now I have a list of words in a txt file. I store this list in an array, and I want to compare the contents of that array to some

Solution 1:

You didn't use jquery, just native javascript.

After your script reading file, just do:

$(searchinput).on("keypress", function() {
   if ($.inArray(formInput,myArray) > -1) {
      alert("There's a match!");
   }
});

UPDATE

$(searchinput).on("blur", function() {
   if ($.inArray(formInput,myArray) > -1) {
      alert("There's a match!");
   }
});

Solution 2:

You need to search the entire array, not just compare it to the value:

if ($.inArray(formInput,myArray)>=0) { // returns -1 if no matchalert("There's a match!");

http://api.jquery.com/jQuery.inArray/

Solution 3:

Loop through the array and match the input value with array elements like :

for(var i in myArray) {
       var arrayElement = myArray[i];
       if (arrayElement == formInput) {
            //Do your stuff
       }
   }

Post a Comment for "Javascript/jquery Compare Input Value To Array"