Last active
October 27, 2016 12:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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