I am currently developing a digital library where each catalog "card" is represented by a specific string format:
'<div class="cardBox col-lg-4 col-md-6 col-sm-12"><div class="card" style="margin-bottom: 1em;"><img class="card-img-top" src="' + data.values[n][5] + '" alt="Card image cap"><div class="card-body"><h5 class="card-title">' + data.values[n][0] + '</h5><h6 class="doctype card-subtitle mb-2 text-muted">' + data.values[n][3] + '</h6><p class="card-text"><span class="font-weight-bold">Document Part #:</span> ' + data.values[n][1] + '<br /><span class="font-weight-bold">Rev:</span> ' + data.values[n][4] + '<br /><span class="font-weight-bold">Language:</span> <span class="language">' + data.values[n][10] + '</span><br /><span class="font-weight-bold">Customer:</span> <span class="customer">' + data.values[n][2] + '</span><br /><span class="font-weight-bold">Date Updated:</span> ' + data.values[n][11] + '</p><a href="' + data.values[n][7] + '" class="card-link" target="_blank">View Document</a><a href="' + data.values[n][6] + '" class="card-link" target="_blank">Download</a></div></div></div>'
There is also a search function that scans this string for the input entered into the search field. Here is the code snippet for the search functionality:
function searchByTitle() {
var filter, card, title, i
filter = document.getElementById("searchText").value.toUpperCase().split(" ");
card = document.getElementById("content").getElementsByClassName("cardBox");
console.log(filter);
for (h = 0; h <= filter.length; h++) {
for (i = 0; i < card.length; i++) {
if (card[i].innerHTML.toUpperCase().includes(filter[h]) == true) {
card[i].style.display = "";
} else {
card[i].style.display = "none";
}
}
}
}
However, there is an issue with the search functionality. When searching for multiple substrings separated by spaces, the documents matching all substrings must appear in sequential order within the card. If they do not appear in exact sequence, no relevant documents are displayed.
For instance, searching for: freezer widget thingy
, will only display results if these terms are in exact order within the card content.
How can I modify the search function to find substrings in any random order within the larger string?
EDIT: I attempted to loop through both the text being searched and the array of search terms without success.
EDIT 2: I revised the looping logic as per the above suggestions, but now my search function does not return any results when typing into the search field. What could be causing this issue?