I have a jQuery array with a bunch of values.
I want a user to be able to type into an input and have any partial matches to anything within my array display on the screen.
So far, I've got it to know when there's a complete match, and I could print that to the page. But I'm not sure how to go about partial matches.
Here's a JS Fiddle of what I have so far.
Here's my code just incase:
var ingredients = ["cheese", "chicken", "cherries", "chick peas", "potato"]
var input = $('#ingredient-search');
var value = input.val();
var resultsDiv = '<div class="ingredient-search-results"><h2>Results for "<span class="results-for"></span>"</h2></div>';
var pressed = false;
var resultsFor;
var matches;
$("#ingredient-search").on("keyup", function() {
if(pressed == false ){
$('.append').append(resultsDiv);
}
pressed = true;
resultsFor = $('.results-for');
resultsFor.html($(this).val());
if (jQuery.inArray(resultsFor.html(), ingredients) != -1) {
alert(resultsFor.html() + ' is in the array!');
}
});
Any ideas?