I have this code which now works, well it does when I use the one-dimensional list, as soon as I add the key is doesn’t work and I know why but am so confused on how to fix it.
Any help?
var targetInput = document.getElementById(“aircraft”),
results = document.getElementById(“autocomplete-results”),
//aircraftList = [‘Airbus A320-200′,’Airbus A330-200′,’Airbus A380-800′,’Boeing 737-200′,’Boeing 777-200ER’],
aircraftList = {‘Airbus A320-200’:1,
‘Airbus A330-200’:2,
‘Airbus A380-800’:3,
‘Boeing 737-200’:4,
‘Boeing 777-200ER’:5 },
matches = [];
//targetInput.focus();
//targetInput.addEventListener(“keyup”, function(event){
$(document).on(‘keyup’,’#aircraft’,function(){
$(“#autocomplete-results”).html(“”);
toggleResults(“hide”);
if (this.value.length > 0){
matches = getMatches(this.value);
if (matches.length > 0){
displayMatches(matches);
}
}
});
function toggleResults(action){
if (action == ‘show’){
$(“#autocomplete-results”).addClass(‘visible’);
} else {
$(“#autocomplete-results”).removeClass(‘visible’);
}
}
function getMatches (inputText){
var matchList = [];
for(var i = 0; i < aircraftList.length; i++){
if (aircraftList[i].toLowerCase().indexOf(inputText.toLowerCase()) != -1){
matchList.push(aircraftList[i]);
}
}
return matchList;
}
function displayMatches(matchList){
var j = 0;
while (j < matchList.length){
console.log(‘dd’+ matchList[j]);
$(“#autocomplete-results”).append(‘<li class=”result”>’ + matchList[j] + ‘</li>’);
j++;
}
toggleResults(“show”);
}