Sometimes, we want to add JavaScript autocomplete without an external library.
In this article, we’ll look at how to add JavaScript autocomplete without an external library.
How to add JavaScript autocomplete without an external library?
To add JavaScript autocomplete without an external library, we add the datalist element.
For instance, we write
<datalist id="languages">
<option value="HTML"></option>
<option value="CSS"></option>
<option value="JavaScript"></option>
<option value="Java"></option>
<option value="Ruby"></option>
</datalist>
<input type="text" list="languages" />
to add the datalist element with id attribute value that matches the list attribute value of the input.
As a result, the datalist’s options are shown when we type in something into the input that matches the options.
Conclusion
To add JavaScript autocomplete without an external library, we add the datalist element.