I am really new to programming and trying to make my first site using the basics. I am trying to do a search/filter bar to search/filter through the set of images on my web. Lets go with "Josh". If I search "Josh", it finds it, but if I search "josh" or "JOsh" it does not.
function search(){
var searchText = document.getElementById("searchInput").value;
var images = document.querySelectorAll(".image_container > img");
if(searchText.length > 0){
images.forEach((image) => {
image.classList.add("hide");
if(image.dataset.tags.indexOf(searchText) > -1){
image.classList.remove("hide");
}
});
}else{
images.forEach((image) => {
image.classList.remove("hide");
});
}
}
I tried using the .toLowerCase() before .indexOf but that just makes all the search lowercase and if I try to use it somewhere else it just straight up does not work. Any help would be appreciated.
4
Answers
As stated by Nina Scholz, you need to normalize the string.
Your initial move was partially correct, you stated that you had used
.toLowerCase() before .indexOf
, what you need to do, is to also make the search value input lowercase. Then everything will work as intended.You can’t use
.indexOf()
in this case.You could use the array function
some()
(mdn link).There are many ways of achieving what you want here. Personally, I wouldn’t use
forEach
orsome
at all; they are 100x – 1000x slower than just using afor(){}
loop. (But if you only have a few images with a couple tags or something, it won’t matter.)for strings you can use regular expression that match your input no matter if its lower or upper and use search method instead of indexOf, this is how you can do it:
for arrays you need to use test method instead of search as follows
Make both
upper case
or bothlower case
, then find index of searchText in tag