Coder Social home page Coder Social logo

autocomplete's Introduction

autocomplete

Introduction

A simple Javascript library for creating autocomplete inputs.

Import the autocomplete library:

<script src="js/autocomplete.js"></script>

Initialize an input with autocomplete (array):

var autocomplete_1 = new Autocomplete({
    target: document.querySelector('input[name="test1"]'),
    source: ["red", "green", "yellow"]
});

Initialize an input with autocomplete (array of objects):

var autocomplete_2 = new Autocomplete({
    target: document.querySelector('input[name="test2"]'),
    source: [{ label: "one", value: "1" }, { label: "two", value: "2" }, { label: "three", value: "3" }]
});

Initialize an input with autocomplete (function):

var autocomplete_3 = new Autocomplete({
    target: document.querySelector('input[name="test3"]'),
    prepopulate: true,
    source: function (value) {
        let matches = [];
        let list = ["Baseball","Basketball","Football"];
        for (let i = 0; i < list.length; i++) {
            let x = list[i];
            if (x.toLowerCase().startsWith(value.toLowerCase())) matches.push(x);
        }
        autocomplete_3.populate(matches);
    }
});

Initialize an input with autocomplete (function with fetch):

var autocomplete_4 = new Autocomplete({
    target: document.querySelector('input[name="test4"]'),
    delay: 500,
    minLength: 1,
    source: async function (value) {
        try {
            let response = await fetch('cities.html');
            let list = await response.json();
            let matches = [];
            for (let i = 0; i < list.length; i++) {
                let x = list[i];
                if (x.toLowerCase().startsWith(value.toLowerCase())) matches.push(x);
            }
            autocomplete_4.populate(matches);
        } catch (error) {
            console.log(error);
            alert(error);
        }
    }
});

See code for further details.

autocomplete's People

Contributors

trehoffman avatar

Stargazers

luigi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.