I have looked at Display partial array value matches from user input and tried to achieve the same results.
The difference is the array format. I want the search text value to match the array key, name.
No matter what I type in the text field, it displays "no match found!" and I can never display the names. It seems players[x].name.indexOf() is not recognized. How can I display names?
Here is Fiddle
var players = [
{ "id" : "23012",
"name" : "Scott",
"first" : "Stve",
"last" : "Scott" },
{ "id" : "22904",
"name" : "Phillips, A",
"first" : "Adam",
"last" : "Phillips"},
{ "id" : "45783",
"name" : "Phillips, T",
"first" : "Tom",
"last" : "Phillips" },
{ "id" : "54762",
"name" : "Scobery",
"first" : "Don",
"last" : "Scobery" },
{ "id" : "78903",
"name" : "Phillip",
"first" : "Roger",
"last" : "Phillip"}
]
$("#searchField").on("keyup", function() {
$(".append").empty();
if($("#searchField").val() != ""){
for(var x = 0; x < players.length; x++){
if(players[x].name.indexOf(($("#searchField").val()).toLowerCase()) == 0){
$(".append").append(players[x].name+"<br>");
} else {
$(".append").html("no match found!");
}
}
}
});