search tags javascript fixes

This commit is contained in:
crunk 2022-03-09 00:05:35 +01:00
parent f8560eddd9
commit 6a475024d3

View File

@ -60,18 +60,21 @@ let timeout = null;
searchInput.addEventListener('keyup', function (e) { searchInput.addEventListener('keyup', function (e) {
// Clear the timeout if it has already been set. // Clear the timeout if it has already been set.
clearTimeout(timeout); clearTimeout(timeout);
// Make a new timeout set to go off in 1000ms (1 second) // Make a new timeout set to go off in 1000ms (1 second)
timeout = setTimeout(function () { timeout = setTimeout(function () {
console.log('Input Value:', searchInput.value); console.log('Input Value:', searchInput.value);
searchTags(searchInput.value); if (searchInput.value.length > 2) {
searchTags(searchInput.value);
} else {
clearSearchTags();
}
}, 1000); }, 1000);
}); });
function searchTags(searchInput) { function searchTags(searchInput) {
var tag_ele = document.getElementsByClassName('tags'); let tag_ele = document.getElementsByClassName('tags');
for (var i = 0; i < tag_ele.length; ++i) { for (var i = 0; i < tag_ele.length; ++i) {
let searchText = searchInput.toLowerCase(); let searchText = searchInput.toLowerCase().trim();
let tagtext = tag_ele[i].innerText.toLowerCase(); let tagtext = tag_ele[i].innerText.toLowerCase();
if(searchText.includes(tagtext) || tagtext.includes(searchText)) { if(searchText.includes(tagtext) || tagtext.includes(searchText)) {
addClass(tag_ele[i], "searched"); addClass(tag_ele[i], "searched");
@ -81,3 +84,10 @@ function searchTags(searchInput) {
} }
} }
} }
function clearSearchTags() {
let tag_ele = document.getElementsByClassName('tags');
for (var i = 0; i < tag_ele.length; ++i) {
removeClass(tag_ele[i], "searched");
}
}