Skip to content

mindflowgo/input2tags

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

56 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Input2Tags

Version 3.1.1

Supercedes js-input-tags (v2.01)

Check the CodePen.io basic example; autocomplete example.

Project objective: simple but powerful vanilla ES6 javascript (code: 350 lines) input tag generator for any input fields; with auto-completion lists.

Based off the inspiration work of github.com/rk4bir/simple-tags-input; using his idea and CSS but then rewritten for ES6 and more features. Can record special keys (Meta, Alt, Tab, MouseLeft, VolumeUp, etc) as key presses.

This project is mobile-device friendly.

Demo

Demos & Instructions

Video illustrations: demonstration

special-keys

Options

Only required tags are inputId

  • allowDelete: allow the [x] deleting of tags (default true)
  • allowDuplicates: allow duplicate tags (default false)
  • allowSpaces: allow spaces in tags (default false)
  • allowCustomKeys: enables special character handling (default false)
  • autocomplete: array of auto-complete options
  • initialTags: array of initial tags to display
  • targetEl: target list element (if UL) or parent for the created list element
  • onAdd: a function called before adding the tag text (returns text or modified version)
  • onDelete: a function called before deleting a tag (returns true if allowed, false otherwise)
  • onInput: a function called after new user input received
  • onChange: a function called after change to tags (new tag added, re-arranged, deleted tag)

Methods

With a valid Input2Tags() instance you have these methods:

  • getTags(): get the list of tags created
  • setTags([]): set the list of tags
  • addTag(tag): add a new tag to input tags instance
  • deleteTag(index): remove a tag, the index of it's placement
  • showAutocomplete(query): showes autoComplete with matches for the query
  • hideAutocomplete()
  • destroy(): remove the instance

Usage

There are 3 steps to using it

  1. Include the CSS & JS files. 2a. Have an input box (INPUT) that it will use. 2b. If no tag list (UL) is given, it will create one above the input box; if you want to place it differently, create your own and pass it in.
  2. Run the function: const inputTags = new Input2Tags(inputEl, { ...options });

That's it!

BASIC Example

Check the CodePen.io example.

Step 1 - Include Files (change path to match where they are)

    <head>
        <link rel="stylesheet" href="https://unpkg.com/input2tags@latest/style">
    </head>

    <script type="module">
    import Input2Tags from "https://unpkg.com/input2tags@latest?module"

    ( code will go here )
    </script>

Step 2 - Insert needed HTML into your code

<div>
    <ul id="tagsList"></ul>
    
    <input type="text" id="newTag" spellcheck="false" placeholder="Enter a tag" />
</div>

Step 3 - Run Javascript (to initialize INPUT field)

    const inputEl = document.getElementById('newTag');
    const inputTags = new Input2Tags(inputEl);

Quick example html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap 5 not used by Input Tags -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    
    <!-- Only CSS used by Input2Tags -->
    <link rel="stylesheet" href="https://unpkg.com/input2tags@latest/style.css">
</head>

<body>
    <div class="container mt-5">
        <h1 class="text-center mb-4">Tags Input</h1>
            
        <div class="mb-3">
            <p class="mb-2">Tag List:</p>
            <!-- specify the ul LIST element to show the tags -->
            <ul id="myTagList"><li><strong>List:</strong></li></ul>
            <!-- include the input box to input the tags -->
             <p><i>Type something and press Enter</i></p>
            <input type="text" id="newTag" class="form-control mt-2" spellcheck="false" placeholder="Enter a tag" />
        </div>
            
        <div class="mb-3">
            <p class="mb-2">List results: <strong><span id="tagsData"></span></strong></p>
            <div class="mt-5 d-grid gap-2 d-md-flex justify-content-md-start">
                <button class="btn btn-secondary ms-md-2" onClick="btnAddTag('hello')">Add Tag 'hello'</button>
            </div>
        </div>
    </div>

     <!--Simple tags input implementation-->
     <script type="module">
        import Input2Tags from "https://unpkg.com/input2tags@latest"

        const inputEl = document.getElementById('newTag');
        const input2Tags = new Input2Tags(inputEl, {
            autocomplete: ['apple', 'banana', 'cherry'],
            // initialTags: ['one','two','three'], // pre-populate (1)
            targetEl: document.getElementById('myTagList'), // pre-populate (2)
            onChange: (tags) => document.getElementById('tagsData').innerHTML = tags?.join(',') || ''
        });

        // show initial tags by adding something
        setTimeout( ()=>input2Tags.addTag('Auto-Add'), 100)

        // export module functions for DOM
        window.btnAddTag = (tag) => input2Tags.addTag(tag);
     </script>
</body>
</html>        

AutoComplete Example

Check the CodePen.io example.

Step 1 - Include Files (change path to match where they are)

    <head>
        <link rel="stylesheet" href="https://unpkg.com/input2tags@latest/style">
    </head>

    <script type="module">
    import Input2Tags from "https://unpkg.com/input2tags@latest?module"
    </script>

Step 2 - Insert needed HTML into your code

Really just having an input box to enter tags is all that is needed. The package can use an existing list (UL) otherwise it will create one and pre-pend above the INPUT box.

<div>
    <input type="text" id="newTag" spellcheck="false" placeholder="Enter a tag" />

    <button onClick="btnAddTag('hello')">Add Tag 'hello'</button>
</div>

Step 3 - Run Javascript (to initialize INPUT field)

    const inputEl = document.getElementById('newTag');
    const inputTags = new Input2Tags(inputEl, {
        autocomplete: ['apple', 'banana', 'cherry'],
        initialTags: ['one','two','three'], // pre-populate (1)
        // targetEl: document.getElementById('myTagList'), // pre-populate (2)
        // onChange: (tags) => document.getElementById('tagsData').innerHTML = tags?.join(',') || ''
    });

    // export module functions for DOM access if needed
    window.inputTags = inputTags;

Advanced Ideas

You can use the 4 hooks to limit characters allow in inputs, prevent certain tags from being created, or others from being deleted (with these hooks: onInput, onAdd, onDelete)

    const inputEl = document.getElementById('newTag');
    const inputTags = new Input2Tags(inputEl, {
        targetEl: document.getElementById('myList'),
        autocomplete: ['apple', 'banana', 'cherry', 'pear', 'pineapple'],
        // allowCustomKeys: true,
        // onInput: (value,e) => customKeyHandling(value,e),
        onInput,
        onAdd,
        onDelete,
        onChange: (tags) => document.getElementById('tagsOutput').value = tags?.join(',') || '',
        });
  • AutoComplete: Triggering display of autocomplete: see example/advanced.html "Show AutoComplete" button.
    <button onClick="showList('apple')">Show AutoComplete List</button>
    window.showList = (search) => inputTags.showAutocomplete(search);

If you modify the javascript in src/inputtags.js, you can build it with npm run build for distribution.

Before forking and spreading another version, please contact the author with a PR if you have improvements you would like. I'd be happy to integrate improvements.

About

Simple and powerful vanilla javascript plugin to create TAGS from input (and autocomplete)! 🌐 πŸš€

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 48.1%
  • JavaScript 46.8%
  • CSS 5.1%