I am trying to make an autocomplete functionality and want to apply focus to the first item in a list of matches once the user pressed the 'down' arrow.
This fires on keyup within the search box and manages the type of keypress fine:
var handleKeyCode = function (e) {
switch (e.which) {
// Arrow up
case 38:
alert("Up");
break;
// Arrow down
case 40:
console.log("This fires fine...");
arrowDown();
break;
// Other keys
default:
filter();
};
e.preventDefault();
};
This fires once the down arrow has been pressed, however, DOESNT apply the focus code for some reason. When I console log the selector it is returning the correct list item, just failing to focus.
var arrowDown = function () {
console.log("Code below finds the selector fine:");
console.log($('.scheme-list li:visible:first'));
$('.scheme-list li:visible:first').focus(function () {
console.log("The selector is not focused AND...");
console.log("Anything in here is not fired!");
});
};