How can you make an exact search that when you type, for example, the letter "b", it gave out a word that starts with the first letter B, and did not search for that letter all over the word like now js:

$("#searchCity").keyup(function() { var input, filter, container, elem, i, place, overlay; input = this; filter = input.value.toUpperCase(); container = document.getElementById("searchCityResult"); elem = container.getElementsByClassName("city-options"); document.querySelector(".search-city .custom-input").classList.add("cs-active"); for (i = 0; i < elem.length; i++) { place = elem[i].getElementsByClassName("name")[0]; if (place.innerHTML.toUpperCase().indexOf(filter) > -1) { elem[i].style.display = "block"; } else { elem[i].style.display = "none"; } } }); 

    1 answer 1

    Replace
    if (place.innerHTML.toUpperCase().indexOf(filter) > -1)
    on
    if (place.innerHTML.toUpperCase().startsWith(filter))

    Do not forget to fasten the polyfill from here if you are not using transpilers.