Bootstrap5-tags - Replace select[multiple] with nices badges

Overview

Tags for Bootstrap 4/5

NPM Downloads

How to use

An ES6 native replacement for select using standards Bootstrap 5 (and 4) styles.

No additional CSS needed! Supports creation of new tags.

import Tags from "./tags.js";
Tags.init();
// Tags.init(selector, opts);
// You can pass global settings in opts that will apply
// to all Tags instances

By default, only provided options are available. Validation error will be displayed in case of invalid tag.

<label for="tags-input" class="form-label">Tags</label>
<select class="form-select" id="tags-input" name="tags[]" multiple>
  <option disabled hidden value="">Choose a tag...</option>
  <option value="1" selected="selected">Apple</option>
  <option value="2">Banana</option>
  <option value="3">Orange</option>
</select>
<div class="invalid-feedback">Please select a valid tag.</div>

Use attribute data-allow-new to allow creation of new tags. Their default value will be equal to the text. Since you can enter arbitrary text, no validation will occur.

<select class="form-select" id="tags-input" name="tags[]" multiple data-allow-new="true"></select>

Use attribute data-show-all-suggestions in order to show an unfiltered list of options. Only the first matching value is selected.

<select class="form-select" id="tags-input" name="tags[]" multiple data-show-all-suggestions="true"></select>

Use attribute data-allow-clear in order to add a cross to remove items.

<select class="form-select" id="tags-input" name="tags[]" multiple data-allow-clear="true"></select>

Use attribute data-suggestions-threshold to determine how many characters need to be typed to show the dropdown (defaults to 1).

<select class="form-select" id="tags-input" name="tags[]" multiple data-suggestions-threshold="0"></select>

Use attribute data-regex to force new tags to match a given regex.

<select class="form-select" id="tags-input" name="tags[]" multiple data-regex=".*@mycompany\.com$"></select>

Use attribute data-separator to split new tags based on a given separator. You can add multiple separators with |.

<select class="form-select" id="tags-input" name="tags[]" multiple data-separator=" |,">
    <option disabled hidden value="">Type a tag...</option>
</select>

Use attribute data-max to only allow a specific number of tags

<select class="form-select" id="tags-input" name="tags[]" multiple data-max="2" data-allow-clear="true">
    <option disabled hidden value="">Type a tag...</option>
</select>

NOTE: don't forget the [] if you need multiple values!

Server side support

You can also use options provided by the server. This script expects a json response that is an array or an object with the data key containing an array.

Simply set data-server where your endpoint is located. It should provide an array of value/label objects. The suggestions will be populated upon init except if data-live-server is set, in which case, it will be populated on type. A ?query= parameter is passed along with the current value of the searchInput.

<label for="validationTagsJson" class="form-label">Tags (server side)</label>
<select class="form-select" id="validationTagsJson" name="tags_json[]" multiple data-allow-new="true" data-server="demo.json" data-live-server="1">
  <option disabled hidden value="">Choose a tag...</option>
</select>

Accessibility

You can set accessibility labels when passing options:

  • clearLabel ("Clear" by default)
  • searchLabel ("Type a value" by default)

Tips

  • You can also use it on single selects! :-)
  • Use arrow down to show dropdown (and arrow up to hide it)
  • If you have a really long list of options, a scrollbar will be used
  • Access Tags instance on a given element with Tags.getInstance(mySelect)

Bootstrap 4 support

Even if it was not the initial idea to support Bootstrap 4, this component is now compatible with Bootstrap 4 because it only requires minimal changes.

Check out demo-bs4.html

Standalone usage

Obviously, this package works great with the full bootstrap library, but you can also use it without Bootstrap or with a trimmed down version of it

In my version, I'm using bootstrap.native to keep the bundle size under a reasonable size (15,2kb vs 9,5kb for the original version).

You can check out the .scss file to see how to reduce bootstrap 5 css to a smaller size.

Check out demo-standalone.html

Demo

https://codepen.io/lekoalabe/pen/ExWYEqx

How does it look ?

screenshot

Browser supports

Modern browsers (edge, chrome, firefox, safari... not IE11). Add a warning if necessary.

I need more

Maybe you can have a look at https://github.com/Honatas/multi-select-webcomponent

Comments
  • Required field and click enter mobile chrome

    Required field and click enter mobile chrome

    I liked the script, but I noticed some errors when implementing

    1. Not working required, I saw the commit https://github.com/lekoala/bootstrap5-tags/commit/1447a5566b5617b968906dccc58259c1c3f8fa35 Adding this attribute does not display information about what is required

    2. In the mobile version of chrome android, inserting a tag by clicking on enter does not work Although you wrote that you fixed https://github.com/lekoala/bootstrap5-tags/releases/tag/1.4.17 I looked at the demo page

    3. If you start writing text in the input field, then select it with the mouse and continue writing (rewriting the text), then the text is not written, the first time this error is reproduced immediately

    4. When you click on the [label for="id"] or when clicking on the field the highlight is not activated like on classes (blue frame) respectively, it is not possible to apply classes is-valid is-invalid

    You didn’t think maybe implementing NOT through a [select] could solve most of the problems as an example here https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/

    opened by visavi 13
  • Keeping form inputs after sending

    Keeping form inputs after sending

    Hi and thanks again for this awesome plugin. I'm trying to keep the inputs a user makes in the tags input field after sending the form. For simple text input fields, adding value="{{ request.form['id of the field'] }}" keeps the values after input. Is there a way to achieve something similar fot the tags inputs? Would have to be applicable to newly added tags. Thanks a lot!

    opened by staminem 12
  • The tags input field width is too short when Non-ASCII value was input.

    The tags input field width is too short when Non-ASCII value was input.

    The width looks fine when English was input, but when you input Korean or some other foreign language the tags input field shows shorter than its supposed to be

    [Image] image image

    [My Solution] Looks natural when 'length + 1' was replaced to 'length * 2' in _adjustWidth expression

    opened by neko0318w 9
  • improve suggestions drop-down position

    improve suggestions drop-down position

    The current suggestions drop-down position is right under letters entered by user. There are some issues with this:

    1. using keepOpen, the drop-down list position changes/jumps on screen after each selection.
    2. when entering text towards the end of input, the dropdown goes out of screen: 2022-03-17_15-30

    Suggested fix: keep drop-down in a fixed position, always under the input, similar to https://vue-multiselect.js.org/ This would solve both issues. 2022-03-17_15-36

    opened by mariusa 7
  • Displaying selected items when using the data-selected option

    Displaying selected items when using the data-selected option

    We use the following code:

    <select
        class="form-select w-auto"
        id="new-task-regions"
        name="new-task-regions"
        multiple=""
        data-server="https://mocki.io/v1/0c5b5251-b7d6-4be8-9d11-304fd930171a"
        data-allow-clear="true"
        data-clear-end="true"
        data-live-server="true"
        data-full-width="true"
        data-suggestions-threshold="0"
        data-show-all-suggestions="true"
        data-keep-open="true"
        data-selected="1"
        style="display: none"
      ></select>
    

    View on Codepen

    The pre-selected tag from data-selected attribute is not displayed during initialization. It is displayed only after entering any character.

    How fix it?

    opened by pnr32 6
  • Allow custom classes

    Allow custom classes

    I thought badgeStyle would allow me to change the color of the badges, but it did not. I think it would be interesting to add the functionality for own classes or styles, and that badgeStyle would not have a default value.

    After seeing issue #39 I thought it would be possible by using badgeClass, but it didnt work.

    let opt = this._selectElement.querySelector('option[value="' + value + '"]');
    if (opt) {
         data = opt.dataset;
    }
    ...
    classes.push(...data.badgeClass.split(" "));
    ...
    span.classList.add(...classes);
    

    As far as I can see in the code, the badgeClass attribute is obtained from the option tag instead of the select tag. Is this behavior correct? In my case I don't have option tags, just the select element.

    <select class="form-select" id="validationTagsNew" name="tags_new[]" multiple data-badge-class="testing" data-allow-new="true"></select>

    Best regards.

    opened by un0tec 6
  • I want to update the options of the select(s) dynamically and then re-init or update the instances.

    I want to update the options of the select(s) dynamically and then re-init or update the instances.

    This plugin workes so well but I ran into a problem. I want to dynamically update the options of the select(s) as soon as the the change event on the select has fired. In fact, I update the disabled attribute of some options. And then I wanted to re-run the Tags.init() because there is no update() function. So I removed the tags from the DOM and then ran init(). The problem is, that within the INSTANCE_MAP the firstly created instances still exists, so init() will skip the creation of new Objects. Any suggestion? Maybe @lekoala adds a static update() function? That would be the cleanest way.

    opened by geerdes 5
  • Form returning only first value/tag to python

    Form returning only first value/tag to python

    Hi, thanks so much for this simple yet awesome feature. I have one small issue when using it. I want to use it solely for entering "new" tags, so without a "lookup" functionality, within a form in html. I want to pass the data entered to python back-end. However, python only receives the very first value/tag as opposed to the whole list of tags entered. Any idea what may be causing this? Thanks in advance! Stefan

    opened by staminem 5
  • Dropdown doesn't render when only 1 result is returned from live server source

    Dropdown doesn't render when only 1 result is returned from live server source

    Hi,

    I'm trying to use Tags as a user-friendly field for identifying a particular, single database record.

    A simple example might be a private messaging system, which searches a database of usernames. If multiple results are returned, the dropdown menu with all valid suggestions renders correctly, but if only one is found it doesn't render at all - no .show is generated - and the containing .form-control.dropdown gets .is-invalid added.

    <select multiple name="person" data-placeholder="Start typing"
                            id="suggestfield"
                            data-server="/user-suggest/"
                            data-live-server="1"
                            data-suggestions-threshold="3"
                            data-allow-clear="1"
                            data-full-width="1"
                            data-max="1">
                            <option disabled hidden value="">Choose a person</option>
                        </select>
    import Tags from "/js/tags.js";
    Tags.init("#suggestfield");
    

    I've simplified a few values for explanation, but that is otherwise my code. Correctly formed JSON is returned, and no errors are reported in console; it simply seems to be not liking having only one single possible suggestion. I've updated whatever I may have been using to 1.4.34 with no change.

    Post-thought: Could it be that the queried string is not necessarily within the returned suggestion text, as I am also searching an AKA table, but only returning the root text value?

    opened by AaronMLB 4
  • [enhancement] Server side: support custom column names

    [enhancement] Server side: support custom column names

    Hi,

    Server side should provide an array of value/label objects.

    Would it be possible to please have new constructor options which say what server-side fields map to value/label? eg valueField: 'id', labelField: 'title'

    Thanks!

    opened by mariusa 4
  • [Issue] No need to keep the selection popup open if max = 1

    [Issue] No need to keep the selection popup open if max = 1

    On a recent enhancement request it was asked to keep the selection popup open when the multiple attribute is present on a select element. I found a small glitch on how the code was implemented: if multiple attribute is set but only one selection possible (max option set to 1) there's no need to keep displaying the popup since only one selection possible and it was already selected.

    To replicate:

    1. Set the multiple attribute on a select element.
    2. Set the max option to 1, wither by data attributes or global options.
    3. Load the website and select one option from the affected element.
    4. The selection popup remains open but only one option is allowed.

    A side effect issue of this, is that if you switch options from this dropdown a couple times, the popup will remain open no matter what, even if another popup is triggered from a different element.

    opened by jestrella04 4
Releases(1.5.1)
Owner
Thomas Portelange
Web developer
Thomas Portelange
Make use of your favicon with badges, images or videos

favico.js More info here. Author Miroslav Magda Version 0.3.9 Contributors: Serge Barysiuk, pissflaps, Yaroslav Yakovlev, LoicMahieu, Renan Gonçalves,

Miroslav Magda 8.7k Dec 21, 2022
💬 A Twitch (BTTV and FFZ) emotes and badges parser - built with tmi.js in mind

?? A Twitch (BTTV and FFZ) emotes and badges parser - built with tmi.js in mind

Lucas Fernandes 8 Sep 2, 2022
Useful GitHub Badges and based on serverless! 🚀

serverless-github-badges This project is inspired by puf17640/git-badges, but built with the serverless stack: Cloudflare Workers and Cloudflare Worke

Zhou Zhiqiang 38 Dec 20, 2022
This plugin integrates by default with Twitter bootstrap using badges to display the maximum lenght of the field where the user is inserting text. Uses the HTML5 attribute "maxlength" to work.

Bootstrap MaxLength This plugin integrates by default with Twitter bootstrap using badges to display the maximum length of the field where the user is

Maurizio 772 Dec 25, 2022
VanillaSelectBox - A dropdown menu with lots of features that takes a select tag and transforms it into a single or multi-select menu with 1 or 2 levels

vanillaSelectBox v1.0.0 vanillaSelectBox : v1.00 : Adding a package.json file New : Possibility to change the dropdown tree and change the remote sear

philippe MEYER 103 Dec 16, 2022
Converts select multiple elements into dropdown menus with checkboxes

jquery-multi-select Converts <select multiple> elements into dropdown menus with a checkbox for each <option>. The original <select> element is hidden

mySociety 22 Dec 8, 2022
Pure JavaScript (VanillaJS) dropdown menu, with multiple select and searching support

JS Select Pure JavaScript (VanillaJS) dropdown menu, with multiple select and searching support How to use To use the select plugins, two main file mu

Luigi Verolla 4 Mar 17, 2022
a jQuery plugin to provid multiple-select function through 2 List-Boxes

bala.DualSelectList bala.DualSelectList is a jQuery plugin to provid multiple-select function through 2 List-Boxes. Demo page: Single Element: https:/

null 11 Nov 1, 2022
Uneasy is a Web Application where you can select and remove multiple followings from your twitter.

uneasy by Club Gamma Note To add new feat create your branch ?? Prerequisites Before contributing or adding a new feature, please make sure you have a

Club Gamma 4 Sep 29, 2022
Replace ansi escape sequences with tokens indicating what they are

Replace ansi escape sequences with tokens indicating what they are

Lily Scott 3 Mar 7, 2022
Find in note Trilium widget to replace the crappy ctrl+f search.

Trilium-FindWidget Find in note Trilium widget to replace the crappy ctrl+f search. Video sandbox.-.Trilium.Notes.2022-04-22.15-41-19_rescaled.mp4 Fea

Antonio Tejada 8 Nov 22, 2022
Easily add emoji support to your website! Replace keywords with emoji's :yum:

Emoji-Parser.js ?? A emoji parser to easily add emoji support to your website Show your support! Features: Use's the same style GitHub does! TON's of

Marketing Pipeline 13 Sep 9, 2022
It's an alert library build with JavaScript. You can replace your traditional JavaScript alert, confirm and toast with the library.

asteroid-alert It's an alert library build with JavaScript. You can replace your traditional JavaScript alert, confirm with the library. It has also e

Khan Md Sagar 4 Mar 12, 2021
True P2P concept for your p2p powered website/app/client. MSC/MEP (Multiple Strategy Concept/Multiple Entry Points)

TRUE P2P CONCEPT - Lets redecentralize the web This repo is just conceptual. Active development of the endproduct (TRUE P2P) happens here https://gith

Bo 6 Mar 29, 2022
⚡🚀 Call multiple view functions, from multiple Smart Contracts, in a single RPC query!

ethers-multicall ⚡ ?? Call multiple view functions, from multiple Smart Contracts, in a single RPC query! Querying an RPC endpoint can be very costly

Morpho Labs 20 Dec 30, 2022
A package to toggle properties of your HTML tags.

Toggler A package(atleast the code) to toggle properties of tags. I mostly use toggle classes while making a switch theme method, button interaction e

chandra sekhar pilla 6 Jan 9, 2022
Wrap selected text in custom tags with shortcuts.

Obsidian Wrap With Shortcuts Wrap the selected text in customized tags with shortcuts. Underline is provided with Ctrl-u(Cmd-u) as default wrappers. P

Manic Chuang 24 Dec 28, 2022
A Zotero add-on that scans your Markdown reading notes, tags the associated Zotero items, and lets you open notes for the Zotero items in Obsidian.

Zotero Obsidian Citations Adds colored tags to Zotero items that have associated Markdown notes stored in an external folder. Open an associated Markd

Dae 210 Jan 4, 2023
Obsidian plugin that adds autocomplete and auto-formatting to frontmatter tags.

Obsidian Front Matter Tag Wizard Tired of having to type # to get tag autocompletion in your Obsidian note front matter? I feel your pain. This plugin

Eric 10 Nov 5, 2022