Options
Pass these as the second argument to the Input2Tags constructor.
| Option | Type | Default | Description |
|---|---|---|---|
allowDelete |
boolean |
true |
Allow the [x] delete buttons on tags. |
allowDuplicates |
boolean |
false |
Allow duplicate tag names. |
allowSpaces |
boolean |
false |
Allow spaces in tags (requires comma to submit). |
allowCustomKeys |
boolean |
false |
Handle special keys (Meta, Alt, etc.) via onInput. |
autocomplete |
string[] |
[] |
Array of auto-complete suggestions. |
initialTags |
string[] |
[] |
Initial tags to display on load. |
targetEl |
HTMLElement | null |
null |
Optional UL element to render tags into. |
onAdd |
(tag: string) => string |
null |
Function: transform or block tag before adding. |
onDelete |
(tag: string) => boolean |
null |
Function: return false to prevent deletion. |
onInput |
(value: string, event?) => string |
null |
Function: handle raw input (e.g., special keys). |
onChange |
(tags: string[]) => void |
null |
Function: called whenever tags change. |
Methods
Available on an Input2Tags instance.
Returns the current list of tags.
const tags = input2Tags.getTags();
// → ['apple', 'banana']
Replace all tags with a new array.
input2Tags.setTags(['cherry', 'date']);
Add a new tag to the list.
input2Tags.addTag('elderberry');
Remove a tag by its index.
input2Tags.deleteTag(0); // removes first tag
Manually show autocomplete matches.
input2Tags.showAutocomplete('app');
Hide the autocomplete dropdown.
input2Tags.hideAutocomplete();
Clean up instance and remove DOM elements.
input2Tags.destroy();