Fix error when searchbox is missing and move minimum character requirement

This commit is contained in:
Ted Pier 2026-07-07 22:41:33 -07:00
parent 9b62062eba
commit 4a57c21609

View File

@ -11,7 +11,7 @@ function resetSuggestions() {
selectedSuggestion = -1; selectedSuggestion = -1;
} }
searchbox.addEventListener("input", function () { searchbox?.addEventListener("input", function () {
// If there is already a request in progress, // If there is already a request in progress,
// abort. The user has made another input, // abort. The user has made another input,
// invalidating the old query. // invalidating the old query.
@ -19,13 +19,6 @@ searchbox.addEventListener("input", function () {
suggestionController.abort(); suggestionController.abort();
} }
// Only continue for queries of at least
// one character.
if (searchbox.value.length < 2) {
resetSuggestions();
return;
}
// Only make a request for suggestions after // Only make a request for suggestions after
// there is a delay of at least 150 milliseconds // there is a delay of at least 150 milliseconds
// between inputs. // between inputs.
@ -36,6 +29,13 @@ searchbox.addEventListener("input", function () {
return; return;
} }
// Only continue for queries of at least
// one character.
if (searchbox.value.length < 2) {
resetSuggestions();
return;
}
// Create the controller which will be // Create the controller which will be
// used to abort this request if another // used to abort this request if another
// is made before it finishes. // is made before it finishes.
@ -74,7 +74,7 @@ searchbox.addEventListener("input", function () {
}, 150); }, 150);
}); });
searchbox.addEventListener("keydown", function (e) { searchbox?.addEventListener("keydown", function (e) {
const currentSuggestions = suggestions.children; const currentSuggestions = suggestions.children;
// Navigate suggestions by key. // Navigate suggestions by key.