shopping-list/js/search.js
2019-05-24 09:41:25 +02:00

33 lines
No EOL
881 B
JavaScript

$(document).ready(function () {
$("#search-recipes").on("keyup", searchRecipe);
$("#clear-search-string").click(clearSearchString);
});
function searchRecipe() {
var searchString = $("#search-recipes").val().toUpperCase();
var recipes = $("#recipes a");
for (var i = 0; i < recipes.length; i++) {
if (recipes[i].innerHTML.toUpperCase().indexOf(searchString) > -1) {
$(recipes[i]).show()
}
else {
$(recipes[i]).hide();
}
}
$("#recipes").children().each(index => {
var container = ($("#recipes").children())[index];
$(container).removeClass("d-none").addClass("d-flex");
if(($(container).find("a:visible")).length==0){
$(container).removeClass("d-flex").addClass("d-none");
}
});
}
function clearSearchString() {
$("#search-recipes").val("");
$("#search-recipes").focus();
$("#search-recipes").keyup();
}