Skip to content

Auto complete as you type in JavaScript

I was looking the w3cschool link for auto complete as you type.

The above only works for the first ordering for names eg. If country name is United States. You need to type Uni and it shows in drop-down. How to make it work if you type any letter eg ted or state so it still shows. United States in drop down.



All you need is do a includes string comparison instead of starts With and you should be all set if you just show the matching country instead f substring of matching text.

if (arr[i].toUpperCase().includes(val.toUpperCase())) {
  /*create a DIV element for each matching element:*/
  b = document.createElement("DIV");
  /*make the matching letters bold:*/
  b.innerHTML = "<strong>" + arr[i] + "</strong>";

User contributions licensed under: CC BY-SA
9 People found this is helpful