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