Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
JS search bar keywords

How can i add keywords to products in my search bar? for example if i want to find "BRIT PREMIUM BY NATURE ADULT L PUI 15KG" i wold like to just type "BRIT ADULT L PUI 15KG" and still find the product, here it's the code i use:

SearchBx.onkeyup = (e)=>{
  console.log(e);
  let userData = e.target.value;
  let emptyArray = [];
  if(userData){
    emptyArray = suggestions.filter((data)=>{
      return data.toLocaleLowerCase().includes(userData.toLocaleLowerCase());
    });
    emptyArray = emptyArray.map((data)=>{
      return data = '<li>'+ data + '</li>';
    });
    console.log(emptyArray);
    Main.classList.add("show");
  }else{

  }
  showSuggestions(emptyArray);
  let allList = AutoBox.querySelectorAll("li");
  // for (let i = 0; i < allList.length; i++) {
  //   allList[i].setAttribute("onclick", "select(this)");
  // }
  for(const el of allList){ // use for..of instead of indexed for
    el.addEventListener('click', (e) => { // attach an event listener instead of using the onclick attribute
      SearchBx.value = e.target.textContent; // can be a separate function like 'select', but whatever 
      ouvrirPage(); // trigger page opening from click event
    }); 
  }
}
// function select(element){
//   let selectUserData = element.textContent;
//   SearchBx.value = selectUserData;
// }
function showSuggestions(list){
  let listData;
  if(!list.length){
    userValue = SearchBx.value;
    listData = '<li>'+ userValue +'</li>'
  }else{
    listData = list.join('');
  }
  AutoBox.innerHTML = listData;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of searching for the entire user input, i splited the input into words, and search for each word separatedly

SearchBx.onkeyup = (e)=>{
  console.log(e);
  let userData = e.target.value;
  let emptyArray = [];
  if(userData){
    emptyArray = suggestions.filter((data)=>{     
        //####################################
        //########### CHANGED HERE ###########
        //####################################
        //return data.toLocaleLowerCase().includes(userData.toLocaleLowerCase());
        let wordsTypedByTheUser = userData.toLocaleLowerCase().split(' ');                
        return wordsTypedByTheUser.every(word => data.toLocaleLowerCase().includes(word));
        //#######################
        //########### END #######
        //#######################
    });
    emptyArray = emptyArray.map((data)=>{
      return data = '<li>'+ data + '</li>';
    });
    console.log(emptyArray);
    Main.classList.add("show");
  }else{

  }
  showSuggestions(emptyArray);
  let allList = AutoBox.querySelectorAll("li");
  // for (let i = 0; i < allList.length; i++) {
  //   allList[i].setAttribute("onclick", "select(this)");
  // }
  for(const el of allList){ // use for..of instead of indexed for
    el.addEventListener('click', (e) => { // attach an event listener instead of using the onclick attribute
      SearchBx.value = e.target.textContent; // can be a separate function like 'select', but whatever 
      ouvrirPage(); // trigger page opening from click event
    }); 
  }
}
// function select(element){
//   let selectUserData = element.textContent;
//   SearchBx.value = selectUserData;
// }
function showSuggestions(list){
  let listData;
  if(!list.length){
    userValue = SearchBx.value;
    listData = '<li>'+ userValue +'</li>'
  }else{
    listData = list.join('');
  }
  AutoBox.innerHTML = listData;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda