Skip to content

Creating options ​

Let users add what they typed when it is not in the list.

  1. Events appear here.
html
<select name="tags" multiple data-flow-select data-fs-create>...</select>

When the typed text does not match an existing label, a row "Create “text”" appears at the end. Choose it with a click or Enter. The new option:

  • is added to the list for this page,
  • is added to the native select, so the form posts it with the typed text as both value and label,
  • fires flowselect:create with event.detail.item.

It works for single selects too.

Saving on the server first ​

Pass a function to create to save the option and use the value your server returns. Return null to cancel. If the function throws, nothing is added and flowselect:error fires.

js
new FlowSelect('#tags', {
  create: async (label) => {
    const response = await fetch('/api/tags', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ name: label })
    });
    if (!response.ok) {
      return null;
    }
    const tag = await response.json();
    return { value: tag.id, label: tag.name };
  }
});

Translating the row ​

The text comes from messages.create, with {query} as the typed text. See Messages and translation.

Released under the MIT License.