I am currently managing a website with multiple items, each represented by individual div elements. While all items share one common class name, they also have several other class names serving as tags to differentiate them (some tags may overlap between different items).
On the website, I have buttons set up using data-filter=".exampleclass" and data-filter=".exampleclass2", which effectively sort the items based on their class names. Now, I am exploring the idea of implementing a search bar where users can input a class name directly, eliminating the need for separate sorting buttons.
document.getElementById("boxsearch").oninput = function() {
var matcher = new RegExp(document.getElementById("boxsearch").value, "gi");
for (var i = 0; i < document.getElementsByClassName("portfolio-item").length; i++) {
if (matcher.test(document.getElementsByClassName("category")[i])) {
document.getElementsByClassName("portfolio-item")[i].style.display = "inline-block";
} else {
document.getElementsByClassName("portfolio-item")[i].style.display = "none";
}
}
}
Click here for an example in JSFiddle
Currently, I do not have the jQuery file included, so the buttons are not functioning properly. However, they work fine on my end. I just need guidance on how to enable the search functionality to filter items by their class names.
While researching, I found a similar concept that aligns with my goals: View the example here