Tag input

Tags in a form

Press Enter or comma to add. Backspace removes the last.

A limit, and a change event

No tags yet.

Invalid and disabled

Behavior

  • Enter or comma adds the typed text as a tag. Enter never submits the form from this box.
  • Backspace in the empty box removes the last tag. The remove button on a tag does the same for that tag.
  • A paste with commas or new lines is split into tags. A paste of one plain word goes into the box as usual.
  • Leaving the box with text in it adds that text as a tag.
  • A tag that is already there is ignored, and nothing is added past data-max.
  • The text box is added if the markup has none. Add your own when a label needs an id to point at.
  • The hidden input named by data-name holds the tags joined with commas. A form reset restores the start tags.

API

.tag-input is started on load, and so is one added to the page later.

Methods

MethodDescription
tsunami.tagInput.mount(root?)Starts every .tag-input inside root (default: the document) that is not started yet.
tsunami.tagInput.value(el)The tags as an array of strings.
tsunami.tagInput.set(el, list)Replaces the tags. Duplicates are dropped and data-max applies.
tsunami.tagInput.clear(el)Removes every tag.

Events

EventOnDescription
tsunami:tagschangethe .tag-input, bubblesSent when a tag is added or removed. event.detail.value is the array of tags.

Keyboard

KeyDescription
Enter ,Adds the typed text as a tag.
BackspaceIn the empty box, removes the last tag.
TabMoves from the box to the remove buttons and on.

Attributes

Attribute / elementValuesDescription
.tag-inputon a divThe box. Give it role="group" and an aria-label or aria-labelledby.
data-valuecomma-separated textThe tags to start with.
data-maxa numberThe most tags allowed. No limit when left out.
data-namea field nameAdds the hidden input that holds the joined value.
data-disabledpresent or absentDisables the text box and hides the remove buttons.
aria-invalidtrueOn .tag-input: a red border.