I have a function that I use to search for spesific strings in innerText elements with a specific class name. When it finds a match, it gives this element a block display for display, and if no one is displayed, to hide. When I use this to search (I have about 3 thousand items with one class), it takes several seconds (about 5 seconds) to complete it. Is there a more efficient way to write this code and possibly shorten a second or two?
function searchClass(ClassName, Value){
var c = document.getElementsByClassName(ClassName);
var val = Value.toLowerCase().trim();
for(var i=0;i<c.length;i++){
if(c[i].innerText.toLowerCase().trim().indexOf(val) > -1){
c[i].style.display = "block";
}else{
c[i].style.display = "none";
}
}
}
PS. There is a keyup event that fires a function.
EDIT: (solution) My search is instantly accelerated by simply changing the innerText to textContent and some small changes thanks to the comments!
source
share