Turn a text input into a Facebook-style multiple-tag input. Features include:
Suggester is compatible with jQuery 1.5+ and has been unit tested with jQuery 1.9. Download the files in Suggester-1.4.0-Download.zip and copy them to your scripts directory. Include them in your document’s after jQuery is included:
<script src="/js/Suggester.min.js"></script>
<link href="/js/Suggester.min.css" rel="stylesheet" />
Then somewhere in your code, call:
var instance = new $.Suggester(selector, options);
// OR
$(selector).suggester(options);
See the documentation below for a full list of options.
Events can be passed as options to the constructor, or can be added later using jQuery event methods .on()
, .off()
, .bind()
.one()
, .unbind()
and .trigger()
For example:
// Register events in initial options
$(input).suggester({
data: myData,
onBeforeOpen: doStuff
});
// Register events later (OOP style)
var instance = new $.Suggester(selector, {
data: myData
});
instance.on('BeforeOpen', doStuff);
// Register events later (jQuery style)
$(input).suggester({
data: myData
}).suggester('on', 'BeforeOpen', doStuff);
How is data passed to event callbacks?
event
event
is a jQuery event objectevent
also contains useful information related to the event. See the Events section below for more information.event
will have property cancelable
set to true and event.isCancelable()
will return trueevent.preventDefault()
event.stopImmediatePropagation()
event
object will change the behavior of the default actionthis
in the callback will refer to the Suggester instance. See the Instance Properties and Instance Methods sections below for more information.Instance methods may be called using an Object Oriented style or with the classic jQuery style:
// Object Oriented Style
var suggester = new $.Suggester(input, options);
suggester.methodName(arg1, arg2, argN);
// jQuery Style
$(input).suggester(options);
$(input).suggester('methodName', arg1, arg2, argN);
// jQuery Style followed by Object Oriented Style
$(input).suggester(options);
var instance = $(input).suggester('getInstance');
instance.methodName(arg1, arg2, argN);
Use an array of strings for suggestions:
$('.my-text-input').suggester({
data: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
prompt: 'Enter a day of the week'
});
Use ajax to get suggestions:
$('.my-text-input').suggester({
dataUrl: 'http://example.com/myjson?query=%s',
prompt: 'Enter a time zone'
});
Use the OOP pattern and use an array of objects for data
:
var suggester = new $.Suggester('.my-text-input', {
data: [{"label":"John Doe","value":"John Doe <John.Doe@example.com>"}/*,...*/],
onAfterSave: function(event) {
saveToServer(event.newValue);
}
});
suggester.on('AfterClose', doStuff);
suggester.focus();
See the source on the live demos for lots more examples.
Version 1.4.0, Aug 2014
Version 1.3.1, Apr 2014
Version 1.3.0, Mar 2014
git watch
Version 1.2.2, Jul 2013
Version 1.2.1, Jul 2013
Version 1.2.0, Jun 2013
Version 1.1.1, May 2013
Version 1.1.0, May 2013
Version 1.0.2, April 2013
Version 1.0pre, December 2012
After using git to clone the repo, you’ll need nodejs, npm, and grunt-cli installed. See gruntjs.com for more information. Then inside the cloned directory run npm install
and then grunt
Make updates only to the files in the ./src
directory. Then run grunt
to automatically generate documentation and other files. You may also make changes to the demos by editing ./demos/*
files or improve the build process by editing ./Gruntfile.js
. Then make a pull request.
To report bugs, add an issue to the GitHub issue tracker.
Author: kensnyder
Live Demo: http://sandbox.kendsnyder.com/jQuery-Suggester/demos/
GitHub: https://github.com/kensnyder/jQuery-Suggester
#jquery #javascript