Skip to content

Instantly share code, notes, and snippets.

@davorbadrov
Last active October 27, 2016 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davorbadrov/e17aad2cd99d1622ed58247bc3ff909d to your computer and use it in GitHub Desktop.
Save davorbadrov/e17aad2cd99d1622ed58247bc3ff909d to your computer and use it in GitHub Desktop.
Tag and Autocomplete combined - Using Awesomeplete + Taggle libs together to create an autocompleting tag input
// Suppose we have a following HTML element in page:
// <div id="tags" />
const tagsInput = new Taggle(document.querySelector('#tags'), {
allowDuplicates: false,
preserveCase: true,
attachTagId: true,
// tags: preexistingTagsToPrefilTheinput,
// onTagAdd: (e, tag) => {
// console.log('adding tag', tag);
// },
// onTagRemove: (e, tag) => {
// console.log('removing tag', tag);
// }
});
const input = tagsInput.getInput();
const autocomplete = new Awesomplete(input, {
list: ['test1', 'test2', 'test3', 'test4', 'test5'],
filter: Awesomplete.FILTER_STARTSWITH,
autoFirst: true
});
input.addEventListener('awesomplete-selectcomplete', e => {
e.preventDefault();
// on autocomplete, remove the last tag (this prevents duplicate tags due to using Awesomplete and Taggle combined)
var lastTag = tagsInput.getTags().values.slice(-1)[0];
if (lastTag) tagsInput.remove(lastTag.text);
tagsInput.add(e.text.label);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment