DEV Community

How to create an autocomplete input with plain Javascript

Stephen Afam-Osemene on August 13, 2017

Now, most people who have built websites have included forms. Regular text elements, number, select (dropdown), checkboxes and radio buttons. ...
Collapse
 
udus97 profile image
Qudus

Hi, Afam
This is a great article; its good to see this implementation in Vanilla JS. But i'd like to know the reason why you are testing for this condition in your JS file

!isNaN(input.value)

Thanks.

Collapse
 
stephenafamo profile image
Stephen Afam-Osemene

Thanks for spotting that. There's actually no reason for that in the example.
It was specific code for a project.

I'll edit.

Collapse
 
moe64 profile image
Moe

Thank you! I need to do an autocomplete form and I had no idea where to start! 🔑

Collapse
 
stephenafamo profile image
Stephen Afam-Osemene

You're welcome!!!

I'm really happy that this helped

Collapse
 
simo97 profile image
ADONIS SIMO

this saved my day... thanks a lot

Collapse
 
simo97 profile image
ADONIS SIMO

I've made this after reading you tutorial, github.com/simo97/Nefertiti,

it is moslty for the case you already has an existing app with , the goal here is to turn them into lightweight autocomplete input. No dependencies, plain vanilla js, based on as you teach me.

thanks it save me from a lot of refractoring on my current project.

Collapse
 
stephenafamo profile image
Stephen Afam-Osemene

That's great! Thanks for the feedback. I'll check your project out.

Collapse
 
danhodge profile image
danhodge

Great post - datalist is a really powerful addition to HTML5 that I was not aware of.

Collapse
 
saaage profile image
Stefan Age • Edited

Thanks for this! I'm trying to write a React component that does the same, this will be a huge help.

Collapse
 
tganyan profile image
Tyler Anyan

This is exactly what I've been looking for, so thanks for posting! I am getting a host of issues, however, and I'm a bit of a newbie with some of this (especially php) so I'm not sure what the wrench in the gears is here...

In Chrome: Failed to load file:///C:/query.php?query=N: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

In Chrome with web server:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse ()
at XMLHttpRequest.window.hinterXHR.onreadystatechange (index.js:35)

In Firefox: NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
window.hinterXHR.send();

Collapse
 
mrkezii profile image
David Kezi

Awesome Awesome Post

Collapse
 
singhkumarhemant profile image
Hemant Kumar Singh

Thanks for this awesome article... it was really helpful

Collapse
 
asadskhan profile image
asadskhan

Hi, Afam
This is a great article.

Thanks

Collapse
 
joaovmvini profile image
joaovmvini

Nice article, thanks for sharing! I have build one, check it out: jsuites.net/v3/dropdown-and-autoco...

Collapse
 
spik3s profile image
Jacek 'Spikes' Wozniak

Hi! Great article!

It's worth to mention that this will not work as intended on iOS devices, the dropdown will not show.

Collapse
 
rodhzuniga profile image
ZunHer

great friend

how to make it work only with the initials of each word.