Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom autocomplete list for an input field

Tags:

html

css

input

I need to create a custom auto-complete list of suggestions for an input field. So far I have associated an event handler to the input event to the html input element, so that when that event triggers, I can fetch the suggestions.

The problem is how would I show these suggestions. By default, input elements can display suggestions, but is it possible to customize/access those suggestions?

If not, what would be the alternatives?

Preferably I would like not to use external libraries.

like image 727
nbro Avatar asked Nov 29 '15 16:11

nbro


1 Answers

You may use 'datalist' tag but it doesn't work in IE <= 9

    <!DOCTYPE html>
    <html>
        <body>
            <form>

              <input list="country" name="countru">

              <datalist id="country">

                <option value="U.S.">
                <option value="France">
                <option value="China">
                <option value="Cambodia">
                <option value="Chile">
                <option value="Canada">
                <option value="Poland">

              </datalist>

              <input type="submit">
            </form>
        </body>
    </html>

Run the code and try typing 'C' and then 'a'.

like image 129
Daniel Konopka Avatar answered Oct 18 '22 02:10

Daniel Konopka