showing results for - "search input at array javascript"
Lena
12 Sep 2017
1var people = [
2  {
3    name: "John Smith",
4    url: "http://example.com/johnsmith"
5  },
6  {
7    name: "John Johnson",
8    url: "http://example.com/johnjohnson"
9  },
10  {
11    name: "Bob Thompson",
12    url: "http://example.com/bobthompson"
13  },
14  {
15    name: "Smith Sanchez",
16    url: "http://example.com/smithsanchez"
17  },
18  {
19    name: "Bob Sanchez",
20    url: "http://example.com/bobsanchez"
21  }
22];
23$("#search-input").on("keyup", function(){
24  var searchFor = $("#search-input").val().toLowerCase();
25  var results = [];
26  for(var i=0;i<people.length;i++){
27    if(people[i].name.toLowerCase().indexOf(searchFor) > -1)
28      results.push("<a href='"+people[i].url+"' target='_blank'>"+people[i].name+"</a>")
29  }
30  if(results.length == 0)
31    $("#search-results").html("No Results Found");
32  else
33    $("#search-results").html(results.join("<br>"));
34});