Categories
JavaScript Answers

How to add JavaScript autocomplete without an external library?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *