Convert Figma logo to code with AI

TarekRaafat logoautoComplete.js

Simple autocomplete pure vanilla Javascript library.

3,927
236
3,927
63

Top Related Projects

typeahead.js is a fast and fully-featured autocomplete library

Ultra lightweight, usable, beautiful autocomplete with zero dependencies.

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields

Quick Overview

autoComplete.js is a lightweight and powerful vanilla JavaScript library for creating autocomplete functionality. It offers a simple and customizable solution for adding intelligent search suggestions to input fields, enhancing user experience and improving search efficiency.

Pros

  • Lightweight and dependency-free
  • Highly customizable with numerous configuration options
  • Supports multiple data sources (local and remote)
  • Responsive and mobile-friendly

Cons

  • Limited built-in styling options
  • May require additional setup for complex use cases
  • Documentation could be more comprehensive
  • Limited browser support for older versions

Code Examples

  1. Basic usage with local data:
const autoCompleteJS = new autoComplete({
  selector: "#autoComplete",
  data: {
    src: ["Apple", "Banana", "Cherry", "Date", "Elderberry"],
    cache: true,
  },
  resultsList: {
    element: (list, data) => {
      if (!data.results.length) {
        const message = document.createElement("div");
        message.setAttribute("class", "no_result");
        message.innerHTML = `<span>Found No Results for "${data.query}"</span>`;
        list.prepend(message);
      }
    },
    noResults: true,
  },
  resultItem: {
    highlight: true
  },
  events: {
    input: {
      selection: (event) => {
        const selection = event.detail.selection.value;
        autoCompleteJS.input.value = selection;
      }
    }
  }
});
  1. Using remote data source:
const autoCompleteJS = new autoComplete({
  selector: "#autoComplete",
  data: {
    src: async (query) => {
      try {
        const source = await fetch(`https://api.example.com/search?q=${query}`);
        const data = await source.json();
        return data;
      } catch (error) {
        return error;
      }
    },
    cache: false,
  },
  // ... other options
});
  1. Custom results rendering:
const autoCompleteJS = new autoComplete({
  selector: "#autoComplete",
  data: {
    src: ["Apple", "Banana", "Cherry", "Date", "Elderberry"],
  },
  resultItem: {
    element: (item, data) => {
      item.style = "display: flex; justify-content: space-between;";
      item.innerHTML = `
        <span style="text-overflow: ellipsis; white-space: nowrap; overflow: hidden;">
          ${data.match}
        </span>
        <span style="display: flex; align-items: center; font-size: 13px; font-weight: 100; text-transform: uppercase; color: rgba(0,0,0,.2);">
          ${data.value}
        </span>`;
    },
    highlight: true
  },
  // ... other options
});

Getting Started

  1. Include the autoComplete.js library in your HTML:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/css/autoComplete.min.css">
<script src="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/autoComplete.min.js"></script>
  1. Create an input field with an ID:
<input id="autoComplete" type="text" tabindex="1">
  1. Initialize autoComplete.js:
const autoCompleteJS = new autoComplete({
  selector: "#autoComplete",
  data: {
    src: ["Your", "Data", "Array", "Here"],
    cache: true,
  },
  resultsList: {
    element: (list, data) => {
      if (!data.results.length) {
        const message = document.createElement("div");
        message.setAttribute("class", "no_result");
        message.innerHTML = `<span>Found No Results for "${data.query}"

Competitor Comparisons

typeahead.js is a fast and fully-featured autocomplete library

Pros of typeahead.js

  • More mature and battle-tested, with a larger user base
  • Offers more advanced features like highlighting and custom templates
  • Better suited for large datasets and complex use cases

Cons of typeahead.js

  • No longer actively maintained (last commit in 2017)
  • Heavier and more complex, which may be overkill for simple use cases
  • Requires jQuery as a dependency

Code Comparison

typeahead.js:

$('#search-input').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: substringMatcher(states)
});

autoComplete.js:

new autoComplete({
  selector: "#search-input",
  data: {
    src: states,
    cache: true,
  },
  resultsList: {
    element: (list, data) => {
      if (!data.results.length) {
        const message = document.createElement("div");
        message.setAttribute("class", "no_result");
        message.innerHTML = `<span>Found No Results for "${data.query}"</span>`;
        list.prepend(message);
      }
    },
    noResults: true,
  },
  resultItem: {
    highlight: true
  }
});

Both libraries provide autocomplete functionality, but autoComplete.js offers a more modern and lightweight approach with no dependencies. typeahead.js provides more advanced features out of the box, but at the cost of complexity and reliance on jQuery. The choice between the two depends on project requirements and preferences.

Ultra lightweight, usable, beautiful autocomplete with zero dependencies.

Pros of Awesomplete

  • Lightweight and dependency-free, with a smaller file size
  • Simple API and easy integration into existing projects
  • Supports keyboard navigation out of the box

Cons of Awesomplete

  • Less customizable and fewer advanced features
  • Limited styling options without additional CSS
  • Fewer data source options compared to autoComplete.js

Code Comparison

Awesomplete:

var input = document.getElementById("myinput");
new Awesomplete(input, {
    list: ["Ada", "Java", "JavaScript", "Brainfuck", "LOLCODE", "Node.js", "Ruby on Rails"]
});

autoComplete.js:

new autoComplete({
    selector: "#myinput",
    data: {
        src: ["Ada", "Java", "JavaScript", "Brainfuck", "LOLCODE", "Node.js", "Ruby on Rails"]
    }
});

Both libraries offer simple setup for basic autocomplete functionality. Awesomplete uses a more traditional approach with a constructor and options object, while autoComplete.js uses a configuration object with nested properties.

Awesomplete is ideal for simpler projects requiring basic autocomplete functionality with minimal setup. autoComplete.js offers more advanced features and customization options, making it suitable for complex applications with specific requirements.

The choice between the two depends on project needs, desired features, and performance considerations. Awesomplete is lighter and simpler, while autoComplete.js provides more flexibility and advanced capabilities.

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields

Pros of jQuery-Autocomplete

  • Lightweight and easy to integrate with jQuery-based projects
  • Supports custom data formatting and multiple data sources
  • Extensive documentation and community support

Cons of jQuery-Autocomplete

  • Requires jQuery as a dependency, which may increase overall page load time
  • Less modern and flexible compared to vanilla JavaScript solutions
  • Limited built-in styling options and customization features

Code Comparison

jQuery-Autocomplete:

$('#autocomplete').autocomplete({
    serviceUrl: '/api/suggestions',
    onSelect: function(suggestion) {
        console.log('Selected: ' + suggestion.value);
    }
});

autoComplete.js:

new autoComplete({
    selector: "#autocomplete",
    data: {
        src: async () => {
            const source = await fetch("/api/suggestions");
            return await source.json();
        },
        key: ["value"]
    },
    onSelection: feedback => {
        console.log('Selected: ' + feedback.selection.value);
    }
});

The code comparison shows that jQuery-Autocomplete uses a more concise syntax due to jQuery's chaining, while autoComplete.js offers a more modern, promise-based approach for data fetching and handling. autoComplete.js also provides a more flexible configuration object, allowing for easier customization of various aspects of the autocomplete functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

autoComplete.js :sparkles:

License GitHub package.json version npm 100% Javascript Zero Dependencies Minified Size Yes Maintained npm Open Source Love



autoComplete.js Design



Simple autocomplete pure vanilla Javascript library. :rocket: Live Demo v10

autoComplete.js is a simple, pure vanilla Javascript library progressively designed for speed, high versatility, and seamless integration with a wide range of projects & systems. (Made for a better developer experience)

Features

  • Pure Vanilla Javascript
  • Zero Dependencies
  • Simple & Lightweight
  • Powerful Search Engine with two different modes
  • Diacritics Support
  • Debounce Support
  • Life Cycle Events
  • Useful plugin API
  • WAI-ARIA Compliant
  • Highly Customizable
  • Works on anything (<input>, <textarea> and contentEditable elements)
  • Well Documented

autoComplete.js Code Example

Getting Started

Installation:

CDN

JS

<script src="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/autoComplete.min.js"></script>

CSS

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@tarekraafat/autocomplete.js@10.2.7/dist/css/autoComplete.min.css">

Package Manager

  • npm install (Node Package Manager)
npm i @tarekraafat/autocomplete.js
  • Yarn install (Javascript Package Manager)
yarn add @tarekraafat/autocomplete.js

Documentation:

Community Plugins:


Support

Technical questions and support, please post your question on Stack Overflow using the below tag

General questions or new ideas for autoComplete.js please start a discussion on Github using the below link


Author

Tarek Raafat


License

Released under the Apache 2.0 license.

© 2022 Tarek Raafat

NPM DownloadsLast 30 Days