fix search result multiple hightlight on lost focus

also use set vars instead of fetching element again
This commit is contained in:
Aditya Telange 2021-01-16 13:26:55 +05:30
parent 704921ccd1
commit 8222f2bd11
No known key found for this signature in database
GPG Key ID: 82E844EF3DA99E77
1 changed files with 9 additions and 2 deletions

View File

@ -49,7 +49,7 @@ function activeToggle() {
} }
// execute search as each character is typed // execute search as each character is typed
document.getElementById("searchInput").onkeyup = function (e) { sInput.onkeyup = function (e) {
// run a search query (for "term") every time a letter is typed // run a search query (for "term") every time a letter is typed
// in the search box // in the search box
const results = fuse.search(this.value); // the actual query being run using fuse.js const results = fuse.search(this.value); // the actual query being run using fuse.js
@ -78,6 +78,13 @@ document.onkeydown = function (e) {
let ae = document.activeElement; let ae = document.activeElement;
let inbox = document.getElementById("searchbox").contains(ae) let inbox = document.getElementById("searchbox").contains(ae)
if (ae === sInput) {
var elements = document.getElementsByClassName('active');
while (elements.length > 0) {
elements[0].classList.remove('active');
}
}
if (key === "ArrowDown" && resultsAvailable && inbox) { if (key === "ArrowDown" && resultsAvailable && inbox) {
e.preventDefault(); e.preventDefault();
if (ae == sInput) { if (ae == sInput) {
@ -111,7 +118,7 @@ document.onkeydown = function (e) {
ae.click(); // click on active link ae.click(); // click on active link
} else if (key === "Escape") { } else if (key === "Escape") {
resultsAvailable = false; resultsAvailable = false;
document.getElementById("searchResults").innerHTML = sInput.value = ''; // clear inputbox and searchResults resList.innerHTML = sInput.value = ''; // clear inputbox and searchResults
sInput.focus(); // shift focus to input box sInput.focus(); // shift focus to input box
} }
} }