A tag input built with Tailwind CSS and vanilla JavaScript that turns typed words into removable chips. It is shown as the "Topics" field of an article form, but works just as well for skills, labels or keywords.
How it works
The root is the form.tti-tags. Inside, [data-tti-box] looks like one text field: it holds the chip list ul[data-tti-list] and the real text input [data-tti-input]. Each chip is an li.tti-chip with a data-tti-tag value and a small remove button marked data-tti-remove. Thanks to focus-within utilities the whole box gets a violet ring while typing.
The script listens for keydown on the input. Enter or a comma normalizes the text (trimmed, lowercased, spaces turned into hyphens, 24 characters max) and adds a chip. Pasting a comma-separated list adds every part at once. Backspace in an empty field first marks the last chip with tti-chip--armed and removes it on the second press, so a single keystroke never deletes a tag by accident; Escape cancels. Duplicates and going over the limit are rejected: the box gets the tti-box--shake class and a message appears in [data-tti-message].
The suggestion buttons (data-tti-suggest) add their tag in one click and are disabled once that tag is present. After every change the script updates the 3 / 8 counter in [data-tti-count] and writes a comma-separated list into the hidden input[name="topics"] ([data-tti-value]), so the field submits like any other form control. The submit handler calls preventDefault() and simulates saving with a short timeout; replace that timeout with a fetch() call to send the value to your API.
Customization
data-tti-maxon.tti-tags: change the maximum number of tags; the counter, placeholder and suggestions follow automatically.normalize()inscript.js: remove the.toLowerCase()call or change the allowed characters if your tags are case-sensitive.maxlength="24"on[data-tti-input]and.slice(0, 24)innormalize(): allow longer tag names.CHIP_CLASSinscript.jsand the matching classes on the initial chips: swapbg-violet-600for another color or userounded-mdfor square tags..tti-chip--armedinstyle.css: change the highlight color of a chip that is about to be removed.data-tti-suggestbuttons: replace them with your most-used tags, or delete the whole suggestions block.
Use cases
- Blog and CMS editors: authors add topics to an article by typing and pressing Enter or comma
- Profile forms: collect skills as removable chips with a limit and a counter
- Content tools: pasting a comma-separated list adds all keywords at once
- Admin forms: clickable suggestions add common tags and are disabled once used
- Filtering interfaces: tags are stored in a hidden field so the form submits like any other input
Accessibility
The text input has a real label and is described by the keyboard hint and the error message through aria-describedby. Chips are list items inside a labeled list, and every remove button has an aria-label such as "Remove css". After removing a chip with its button, focus moves to the next chip's remove button or back to the input. Additions and removals are announced in a visually hidden aria-live region together with the new count, and the save result appears in a role="status" element. All buttons show a visible focus ring, and the pop and shake animations are disabled under prefers-reduced-motion.
Usage notes
For several tag inputs on one page, give each input and its hint and message elements unique id values.