Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

display of suggestion elements on the search bar using (javaScript)

Hi I created a result suggestion on my search bar, everything works on the suggestion generation process, but I can’t display the elements obtained on the html page.

when testing the code, it shows me ${resultItem.name}
when debugging the error is on the content of the suggestion variable in the forEach loop.

const articles = [{ name: 'toto' }, { name: 'mamam' }, { name: 'tampon' }, { name: 'TANTON' }];

const rechercheInput = document.getElementById('rechercheInput');
rechercheInput.addEventListener('keyup', function() {
  const input = rechercheInput.value;
  const result = articles.filter(item => item.name.toLocaleLowerCase().includes(input.toLocaleLowerCase()));
  let suggestion = '';
  result.forEach(resultItem =>
    suggestion += '<div class="suggestion">${resultItem.name}</div>'
  )
  document.getElementById('suggestions').innerHTML = suggestion;
})
<form method="get" action="">
  <input type="search" name="recherche" placeholder="product.." id="rechercheInput" required>
  <button class="btn waves-effect waves-light" type="submit">Recherche
            <i class="material-icons right">search</i>
          </button>
  <div id="suggestions"></div>
</form>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use template strings instead of single quotes when appending the element to the suggestion key

The change is here

suggestion +=`<div class="suggestion">${resultItem.name}</div>`
const articles = [
{name:'toto'},
{name:'mamam'},
{name:'tampon'},
{name:'TANTON'}
];

const rechercheInput=document.getElementById('rechercheInput');
rechercheInput.addEventListener('keyup',function(){
const input = rechercheInput.value;
const result = 
articles.filter(item=>item.name.toLocaleLowerCase().includes(input.toLocaleLowerCase()));
let suggestion ='';

result.forEach(resultItem =>
    suggestion +=`<div class="suggestion">${resultItem.name}</div>`
    )
document.getElementById('suggestions').innerHTML=suggestion;
})
   <form method="get" action="">
        <input type="search" name="recherche" placeholder="product.." id="rechercheInput" 
        required>
        <button class="btn waves-effect waves-light" type="submit">Recherche
        <i class="material-icons right">search</i>
      </button>
      <div id="suggestions"></div>
    </form> 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading