Code Monkey home page Code Monkey logo

contenteditable-autocomplete-1's Introduction

contenteditable-autocomplete

Add Autocomplete/Typeahead to contenteditable tag

Build Status Dependency Status devDependency Status

Download / Installation

You can download the latest JS & CSS code here:

Or install via npm

npm install --save contenteditable-autocomplete

The JS code can be required with

var jQuery = require('jquery')
var contenteditableAutocomplete = require('contenteditable-autocomplete')

// init
contenteditableAutocomplete(jQuery)

The CSS code lives at node_modules/contenteditable-autocomplete/contenteditable-autocomplete.css

Usage

<!-- load jquery -->
<script src="jquery.js"></script>


<!-- load contenteditable-autocomplete assets -->
<link rel="stylesheet" type="text/css" href="contenteditable-autocomplete.css">
<script src="contenteditable-autocomplete.js"></script>

<!-- The behaviour is initialzied on first interaction -->
<p>
  <strong>Country:</strong>
  <span contenteditable name="country" placeholder="set country" data-autocomplete-spy></span> |
</p>

<p>
  <strong>Countries:</strong>
</p>
<p contenteditable name="countries" placeholder="set countries" data-autocomplete-spy data-autocomplete-multiple></p>

Suggestions

To pass suggestions for the autocomplete, listen to the autocomplete:request event

$('[name=country]').on('autocomplete:request', function(event, query, callback) {
  var suggestions = getSuggestionsArrayFor(query);
  callback(suggestions);
})

Instead of strings, you can also pass objects with the mandatory properties label and value. label will be shown as suggestions. When selected, value will be added to the input.

$('[name=country]').on('autocomplete:request', function(event, query, callback) { callback([ {label: 'Germany (Europe)', value: 'Germany'}, {label: 'Thailand (Asia)', value: 'Thailand'}, {label: 'Uruguay (South America)', value: 'Uruguay'} ]); })

Select event

To react on when a suggestion has been selected, listen to the autocomplete:select event.

$('[name=country]').on('autocomplete:select', function(event, selected) {
  console.log('selected item:', selected);
})

Selected is always an object with label and value properties (see above). Additional properties passed to suggestions will be passed.

Local Setup

git clone [email protected]:gr2m/contenteditable-autocomplete.git
cd contenteditable-autocomplete
npm install

Test

You can start a local dev server with

npm start

Run tests with

npm test

While working on the tests, you can start Selenium / Chrome driver once, and then tests re-run on each save

npm run test:mocha:watch

Fine Print

The Expandable Input Plugin have been authored by Gregor Martynus, proud member of the Hoodie Community.

License: MIT

contenteditable-autocomplete-1's People

Contributors

gr2m avatar npmcdn-to-unpkg-bot avatar

Watchers

 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.