Skip to main content
Login

Sign in to access your account

Tag Input - National Design System

A form field that turns typed text into removable chips and submits them as an array

Overview

A tag input is a text field that holds its values as Chips. The user types a value and presses Enter or a comma, and the value becomes a chip before the text box. The script keeps one hidden input per tag, so the form submits the tags as an array.

Pick another component when:

  • the user picks from a fixed list: Multiselect
  • the user types one value: a text field in Forms
  • the user picks one value from suggestions: Autocomplete

Markup

Options
Structure
Chip color
State
More
Preview
<div class="nds-form-container nds-taginput" data-taginput-name="skills"> <div class="nds-form-header"> <label for="taginput-skills"><span class="nds-label">Skills</span></label> </div> <div class="nds-form-control"> <input type="text" id="taginput-skills" placeholder="Add a skill"> </div> </div>

Behavior

Suggestions

data-url on .nds-taginput and autocomplete="on" on the text box add an Autocomplete menu to the field. A picked suggestion becomes a tag, and typed text still becomes a tag on Enter or a comma. The menu opens only when a suggestion matches. When the user leaves the field while the menu is open, the typed text stays in the box. Set the fetch with the Autocomplete attributes, such as data-fetch, data-min-chars and data-display-field.

Strict Mode

data-strict on a field with data-url makes only picked suggestions become tags. On Enter, a comma or a paste, the field shows “Choose from the suggestions” and keeps the text in the box. Leaving the field keeps the text there too. When nothing matches, the menu shows “No results”. Without data-url, data-strict does nothing. addTag() still adds any value.

Prefilled Tags

A form that edits saved data shows the tags the user saved before. For each tag, write one hidden input in .nds-form-control, such as <input type="hidden" name="skills[]" value="Data analysis">. The field turns each one into a chip when it starts, with no script on the page. Without data-taginput-name, the field takes its name from these inputs.

Tag Limit

data-max-tags on .nds-taginput sets how many tags the field takes at most. A tag past the limit is not added, and the field shows “Maximum limit 3”. The message clears at the next change. A submit also checks the limit, for tags the markup or a script set past it.

Disabled and Read-only

data-state~="disabled" on .nds-taginput disables the text box, every chip and the hidden inputs, so the tags do not post. With data-state~="readonly", the user cannot type, remove a chip or edit the last tag with Backspace. The tags still post. Code can still change the tags with addTag(), removeTag() and clear().

Validation

data-required on .nds-taginput makes the form need at least one tag. The class nds-required does the same. At submit, an empty field shows “Please add at least one tag”. The error clears at the next change. The form does not check the tag limit at submit: the field blocks extra tags as the user adds them.

Built-in Features

  • Auto-initialization

    Every .nds-taginput on the page starts on load. A click anywhere on the field, except on a chip, puts the cursor in the text box.

  • Flexible Commit Keys

    Enter, a comma or the Arabic comma (،) adds the typed text as a tag. Pasted text splits into one tag per comma or line. Leaving the field adds the typed text. Enter in an empty box submits the form.

  • Backspace to Edit

    Backspace in an empty box moves the last tag back into the box as text. The user fixes a typo and presses Enter again.

  • Form Submission

    The script keeps one <input type="hidden" name="skills[]"> per tag, named from data-taginput-name. The form submits the tags as an array, in the order the user added them.

  • Rejection Feedback

    A tag the field already holds, in any letter case, is not added. The field shows "Already added", and the message clears at the next change.

  • Screen Reader Support

    A screen reader reads each change and each rejected tag, such as "Added Data analysis" or "Already added", in English or Arabic.

  • Removable Chips

    Each chip is a button: a click, Enter or Space removes its tag. Focus moves to the chip in its place, or to the text box after the last one.

  • Programmatic Control

    Each field has an instance with getValues(), setValues(), addTag(), removeTag() and clear(). The nds:taginput:change event fires after each change.

Best Practices

  • Use a tag input for values the user makes up, such as skills, keywords or reference numbers.
  • Give the field a data-taginput-name. With no name and no prefilled tags, the tags do not post.
  • Set data-max-tags to the limit that the server uses. The user then sees the limit as they type, not after submit.
  • Keep tags to one to three words. For longer text, use a textarea.
  • The field rejects a repeat in any letter case, but keeps the case the user typed. Change the case on the server if the stored values must match.
  • Add Suggestions when tags must match a shared list. The user then picks an existing tag instead of typing a new spelling.
  • Say a limit in the label, such as “Topics (up to 3)”, not in the placeholder.
  • For the label, info text, feedback and the required mark, see Forms. They work the same on every field.

API

Data Attributes

Attribute Element Effect
data-taginput-name .nds-taginput The field name. The script names each hidden input with it and []. Without it, the script takes the name from the first prefilled hidden input. It is also name in the change event
data-max-tags .nds-taginput How many tags the field takes at most. See Tag Limit
data-chip-class .nds-taginput The classes on each chip. The default is nds-primary nds-sm. See Chips
data-url .nds-taginput The URL of the suggestions. Needs autocomplete="on" on the text box. See Suggestions
data-strict .nds-taginput with data-url Only picked suggestions become tags. See Strict Mode
data-required .nds-taginput The form needs at least one tag. See Validation
data-state~="filled" .nds-taginput The script sets it when the field holds a tag, and removes it when the last tag goes. No NDS style reads it: it is for your CSS
data-state~="disabled", data-state~="readonly" .nds-taginput Set it yourself. See Disabled and Read-only
data-taginput-value each chip The script writes the tag’s text on each chip it builds. Use it to select one chip in your code or tests

JavaScript

Method Effect
NDS.Taginput.init() Starts every .nds-taginput that has not started. The loader calls it on load. Call it again after you add a field to the page
NDS.Taginput.create(el) Starts one field, and returns its instance. On a field that has started, it returns the same instance. It returns null when the field has no .nds-form-control or no text box in it
NDS.Taginput.destroy(el) Removes the listeners and the chips. The hidden inputs stay, and init() builds the chips from them to start the field again. Call it before you remove the field from the page
NDS.Taginput.validate(el, options) Checks the field and returns { valid, message, count }: a required field needs a tag, and data-max-tags caps the count. Pass { showMessage: false } to check without a message
el.ndsTaginput The instance of a field that has started
NDS.Taginput.getValues(el) Returns the tags in order, or [] when the field is not found. el is the .nds-taginput, its text box, its id or a selector
NDS.Taginput.setValues(el, values) Replaces every tag with the array values and returns true. One nds:taginput:change fires, not one per tag. It returns false, and writes nothing, when addTag() would refuse a tag: an empty tag, a repeat in the list, or more tags than data-max-tags
NDS.Taginput.clear(el) Removes every tag. Returns true, or false when the field is not found
instance.getValues() Returns the tags, in the order they were added
instance.setValues(values) The same as NDS.Taginput.setValues, on a field that has started
instance.addTag(value) Adds one tag. It trims spaces and commas, and rejects a repeat or a tag past the limit with the same message as typing
instance.removeTag(value) Removes the tag with this exact text
instance.clear() Removes every tag
Event Fired on Detail
nds:taginput:change .nds-taginput, and it bubbles { name, values }, after each tag the field adds or removes, the Backspace edit included, and after clear(). name is data-taginput-name, or '' for a field with no name. values is the tags, in order
var field = document.querySelector('.nds-taginput[data-taginput-name="skills"]'); // Show the count next to the field field.addEventListener('nds:taginput:change', function (e) { document.querySelector('#skill-count').textContent = e.detail.values.length + ' skills'; }); // Add a tag from code field.ndsTaginput.addTag('Accessibility');

The full API is in the banner of _js/nds-taginput.js.

Last Modified Date: 01/11/2026 - 05:15 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks