Buenos días estoy buscando una manera de optimizar mi buscador, ya que sólo busca por las primeras palabras. Por ejemplo:
Si ingreso "au" me muestra las palabras que comienzan con au, a mi me gustaria que me muestre todas las que tengan algo que ver con au, como por ejemplo mecanico de autos, etc. Lo que quiero es que no compare sólo las primeras palabras. Sino que por todas en donde exista..
Dejo la funcion javascript de mi buscador, en realidad sólo la parte que interesa:
inp.addEventListener("input", function(e) {
var a, b, i, val = this.value;
/*close any already open lists of autocompleted values*/
closeAllLists();
if (!val) { return false;}
currentFocus = -1;
/*create a DIV element that will contain the items (values):*/
a = document.createElement("DIV");
a.setAttribute("id", this.id + "autocomplete-list");
a.setAttribute("class", "autocomplete-items");
/*append the DIV element as a child of the autocomplete container:*/
this.parentNode.appendChild(a);
/*for each item in the array...*/
for (i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
/*check if the item starts with the same letters as the text field value:*/
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {
/*create a DIV element for each matching element:*/
b = document.createElement("DIV");
b.setAttribute("class", "ayudadorlistaporuno");
/*make the matching letters bold:*/
b.innerHTML = "<strong>" + arr[i].substr(0, val.length) + "</strong>";
b.innerHTML += arr[i].substr(val.length);
Como allí se observa y está comentado:
if (arr[i].substr(0, val.length).toUpperCase() == val.toUpperCase()) {}
Sería la función que tenemos que modificar pero realmente hace días que no logro destrabarme.
Agradezco cualquier ayuda, saludos!
arr[i].toUpperCase().includes(val.toUpperCase())
?