Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
autocomplete.js
Advanced tools
This JavaScript library adds a fast and fully-featured auto-completion menu to your search box displaying results "as you type". It can be easily combined with Algolia's realtime search engine. The library is available as a jQuery plugin.
Turns any input[type="text"]
element into an auto-completion menu. options
is an
options hash that's used to configure the autocomplete to your liking. Refer to
Options for more info regarding the available configs. Subsequent
arguments (*datasets
), are individual option hashes for datasets. For more
details regarding datasets, refer to Datasets.
$('.search-input').autocomplete({
minLength: 3
},
{
name: 'my-dataset',
source: mySource
});
Removes the autocomplete functionality and reverts the input
element back to its
original state.
$('.search-input').autocomplete('destroy');
Opens the dropdown menu of the autocomplete. Note that being open does not mean that the menu is visible. The menu is only visible when it is open and has content.
$('.search-input').autocomplete('open');
Closes the dropdown menu of the autocomplete.
$('.search-input').autocomplete('close');
Returns the current value of the autocomplete. The value is the text the user has
entered into the input
element.
var myVal = $('.search-input').autocomplete('val');
Sets the value of the autocomplete. This should be used in place of jQuery#val
.
$('.search-input').autocomplete('val', myVal);
Returns a reference to the autocomplete plugin and reverts jQuery.fn.autocomplete
to its previous value. Can be used to avoid naming collisions.
var autocomplete = jQuery.fn.autocomplete.noConflict();
jQuery.fn._typeahead = autocomplete;
When initializing an autocomplete, there are a number of options you can configure.
hint
– If false
, the autocomplete will not show a hint. Defaults to true
.
templates
– An optional hash overriding the default templates.
dropdownMenu
– the dropdown menu template. The template should include all dataset placeholders.header
– the header to prepend to the dropdown menufooter
– the footer to append to the dropdown menu<script type="text/template" id="my-custom-menu-template">
<div class="my-custom-menu">
<div class="row">
<div class="col-sm-6">
<div class="aa-dataset-contacts1"></div>
</div>
<div class="col-sm-6">
<div class="aa-dataset-contacts2"></div>
<div class="aa-dataset-contacts3"></div>
</div>
</div>
</div>
</script>
<script>
$('#search-input').autocomplete(
{
templates: {
dropdownMenu: '#my-custom-menu-template',
footer: '<div class="branding">Powered by <img src="https://www.algolia.com/assets/algolia128x40.png" /></div>'
}
},
[
{
source: index.ttAdapter({ hitsPerPage: 5 }),
name: 'contacts1',
templates: {
header: '<h4>List 1</h4>',
suggestion: function(suggestion) {
// FIXME
}
}
},
{
source: index.ttAdapter({ hitsPerPage: 2 }),
name: 'contacts2',
templates: {
header: '<h4>List 2</h4>',
suggestion: function(suggestion) {
// FIXME
}
}
},
{
source: index.ttAdapter({ hitsPerPage: 2 }),
name: 'contacts3',
templates: {
header: '<h4>List 3</h4>',
suggestion: function(suggestion, answer) {
// FIXME
}
}
}
]
);
</script>
minLength
– The minimum character length needed before suggestions start
getting rendered. Defaults to 1
.An autocomplete is composed of one or more datasets. When an end-user modifies the value of the underlying input, each dataset will attempt to render suggestions for the new value.
Datasets can be configured using the following options.
source
– The backing data source for suggestions. Expected to be a function
with the signature (query, cb)
. It is expected that the function will
compute the suggestion set (i.e. an array of JavaScript objects) for query
and then invoke cb
with said set. cb
can be invoked synchronously or
asynchronously.
name
– The name of the dataset. This will be appended to tt-dataset-
to
form the class name of the containing DOM element. Must only consist of
underscores, dashes, letters (a-z
), and numbers. Defaults to a random
number.
displayKey
– For a given suggestion object, determines the string
representation of it. This will be used when setting the value of the input
control after a suggestion is selected. Can be either a key string or a
function that transforms a suggestion object into a string. Defaults to
value
.
templates
– A hash of templates to be used when rendering the dataset. Note
a precompiled template is a function that takes a JavaScript object as its
first argument and returns a HTML string.
empty
– Rendered when 0
suggestions are available for the given query.
Can be either a HTML string or a precompiled template. If it's a precompiled
template, the passed in context will contain query
.
footer
– Rendered at the bottom of the dataset. Can be either a HTML
string or a precompiled template. If it's a precompiled template, the passed
in context will contain query
and isEmpty
.
header
– Rendered at the top of the dataset. Can be either a HTML string
or a precompiled template. If it's a precompiled template, the passed in
context will contain query
and isEmpty
.
suggestion
– Used to render a single suggestion. If set, this has to be a
precompiled template. The associated suggestion object will serve as the
context. Defaults to the value of displayKey
wrapped in a p
tag i.e.
<p>{{value}}</p>
.
The autocomplete component triggers the following custom events.
autocomplete:opened
– Triggered when the dropdown menu of the autocomplete is
opened.
autocomplete:closed
– Triggered when the dropdown menu of the autocomplete is
closed.
autocomplete:cursorchanged
– Triggered when the dropdown menu cursor is moved
to a different suggestion. The event handler will be invoked with 3
arguments: the jQuery event object, the suggestion object, and the name of
the dataset the suggestion belongs to.
autocomplete:selected
– Triggered when a suggestion from the dropdown menu is
selected. The event handler will be invoked with 3 arguments: the jQuery
event object, the suggestion object, and the name of the dataset the
suggestion belongs to.
autocomplete:autocompleted
– Triggered when the query is autocompleted.
Autocompleted means the query was changed to the hint. The event handler will
be invoked with 3 arguments: the jQuery event object, the suggestion object,
and the name of the dataset the suggestion belongs to.
All custom events are triggered on the element initialized as the autocomplete.
Below is a faux mustache template describing the DOM structure of an autocomplete
dropdown menu. Keep in mind that header
, footer
, suggestion
, and empty
come from the provided templates detailed here.
<span class="aa-dropdown-menu">
{{#datasets}}
<div class="aa-dataset-{{name}}">
{{{header}}}
<span class="aa-suggestions">
{{#suggestions}}
<div class="aa-suggestion">{{{suggestion}}}</div>
{{/suggestions}}
{{^suggestions}}
{{{empty}}}
{{/suggestions}}
</span>
{{{footer}}}
</div>
{{/datasets}}
</span>
When an end-user mouses or keys over a .aa-suggestion
, the class aa-cursor
will be added to it. You can use this class as a hook for styling the "under
cursor" state of suggestions.
The project is built using Grunt. To start developing, you can use the following commands:
$ npm install
$ grunt dev
$ open http://localhost:8888/test/playground.html
eslint is used to lint the code based on Algolia's configuration.
$ grunt lint
Unit tests are written using Jasmine and ran with Karma. Integration tests uses Mocha and Saucelabs.
To run the unit tests suite run:
$ npm test
To run the integration tests suite run:
$ grunt
$ grunt server
$ ngrok 8888
$ TEST_HOST=http://YOUR_NGROK_ID.ngrok.com SAUCE_ACCESS_KEY=YOUR_KEY SAUCE_USERNAME=YOUR_USERNAME./node_modules/mocha/bin/mocha --harmony -R spec ./test/integration/test.js
This library has originally been forked from Twitter's typeahead.js library.
0.1.0 July 13, 2015
FAQs
Fast and fully-featured autocomplete library
The npm package autocomplete.js receives a total of 80,678 weekly downloads. As such, autocomplete.js popularity was classified as popular.
We found that autocomplete.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 75 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.