Я пытаюсь создать доступный для поиска контент с помощью некоторого JS, но у меня возникают проблемы с сокрытием контента, когда в поле поиска нет ввода.
Вот мой сценарий:
var $searchContainer = $("#search");
var $contentBoxes = $searchContainer.find(".content");
var $searchInput = $searchContainer.find("#search-input");
var $searchBtn = $searchContainer.find("#search-btn");
$searchBtn.on("click", searchContent);
$searchInput.on("input", searchContent);
while($searchInput == null) {
for($contentBoxes) {
hide();
}
}
function searchContent(){
var userInput;
//Check if call comes from button or input change
if($(this).is(":button")){
userInput = $(this).siblings("input").val();
} else {
userInput = $(this).val();
}
//make the input all lower case to make it compatible for searching
userInput = userInput.toLowerCase();
//Loop through all the content to find matches to the user input
$contentBoxes.each(function(){
var headerText = $(this).find(".title").text();
var contentText = $(this).find(".description").text();
//add the title and content of the contentbox to the searchable content, and make it lower case
var searchableContent = headerText + " " + contentText;
searchableContent = searchableContent.toLowerCase();
//hide content that doesn't match the user input
if(!searchableContent.includes(userInput)){
$(this).hide();
} else {
$(this).show();
}
});
};
Я понимаю, что какое-то время у l oop может быть условие, при котором, если userInput равен нулю, он будет l oop через каждое поле содержимого и скрыть элемент
Нечто подобное может быть?
while($searchInput == null) {
$contentBoxes.each(function(){
hide();
}
}
Любая помощь будет принята с благодарностью.