Steve Kim
Steve Kim

Reputation: 5611

JS search function to show similar search results

I have about 100 terms (no number) saved as variable in js.

var terms = [ALL TERMS];

I have a simple search field: <input type="text" name="search" placeholder="Search..">

I am trying to make it so that when an user starts to type a letter, it automatically brings terms that matches what was typed.

I have seen this kind of function before. I would very much appreciate if someone could point me to a right direction in order to achieve this.

Thanks bunch!

Upvotes: 0

Views: 318

Answers (1)

user6360214
user6360214

Reputation:

If you wish to have a simple search, use the code below:

$("input").on("input",function(){
  for(var c in terms){ // loop through each item in terms
    if(terms[c].replace($(this).val(),"")!=terms[c])
      // the string matches your input
  }
});

Upvotes: 1

Related Questions