Tagin
Simple tag input for Bootstrap. Supports bootstrap v4 and v5.
Demo: https://tagin.netlify.app/
Features
- Custom separators
- Enable/disable duplicates
- Custom transform
- Supports Bootstrap validation style
- Fast
- Small
- No dependencies
Installation
Install tagin with npm:
npm install tagin
Install from cdn:
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/tagin.min.css">
<script src="https://unpkg.com/[email protected]/dist/tagin.min.js"></script>
Usage/Examples
Place css
between <head></head>
tags:
<head>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/tagin.min.css">
</head>
Place js
before </body>
tag:
<body>
...
<script src="https://unpkg.com/[email protected]/dist/tagin.min.js"></script>
</body>
- You can also use tagin as javascript module:
<script src="yourscript.js" type="module"></script>
In yourscript.js
file, import Tagin (Change location according to your path):
import Tagin from './path/to/tagin.module.js'
Or you can use it directly in the html script as a module:
<script type="module">
import Tagin from './path/to/tagin.module.js'
</script>
data-options
attribute needed):
1. Basic Usage (No <input type="text" name="tags" class="form-control tagin" value="red,green,blue">
const options = {
separator: ',', // default: ','
duplicate: false, // default: false
enter: true, // default: false
transform: 'input => input.toUpperCase()', // default: input => input
placeholder: 'Add a group...' // default: ''
}
const tagin = new Tagin(document.querySelector('.tagin'), options)
tagin.addTag('yellow') // Add tag 'yellow'
tagin.addTag(['cyan', 'black']) // Add tags 'cyan' and 'black'
tagin.getTag() // Return tags as string red,green,blue,yellow,cyan,black
tagin.getTags() // Return tags as array ['red', 'green', 'blue', 'yellow', 'cyan', 'black']
2. Using Placeholder
Using data-tagin-placeholder
attribute:
<input type="text" name="tags" class="form-control tagin" value="red,green,blue" data-tagin-placeholder="Add a color... (then press comma)">
Or using option:
const tagin = new Tagin(document.querySelector('.tagin'), {
placeholder: 'Add a color... (then press comma)'
})
3. Using Custom Separator
Example tags with 'space' separator. Using data-tagin-separator
attribute:
<input type="text" name="tags" class="form-control tagin" data-tagin-separator=" " value="red green blue">
Or using option:
const tagin = new Tagin(document.querySelector('.tagin'), {
separator: ' '
})
4. Allow Duplicates
Add data-tagin-duplicate
to remove duplicates validation.
<input type="text" name="tags" class="form-control tagin" data-tagin-duplicate value="html,html,css,css,js,js">
Or using option:
const tagin = new Tagin(document.querySelector('.tagin'), {
duplicate: true
})
5. Transform Tags
Sometimes we need to transform tags. Example tags with toUpperCase()
. Using data-tagin-transform
attribute:
<input type="text" name="tags" class="form-control tagin" data-tagin-transform="input => input.toUpperCase()" value="HTML,CSS">
Or using option:
const tagin = new Tagin(document.querySelector('.tagin'), {
transform: 'input => input.toUpperCase()'
})
6. Force add on enter
Add data-tagin-enter
to force adding tag when enter key is pressed.
<input type="text" name="tags" class="form-control tagin" data-tagin-enter value="red,green,blue" data-placeholder="Add a color... (then press comma or enter)">
Or using option:
const tagin = new Tagin(document.querySelector('.tagin'), {
enter: true
})
Using tagin class on multiple inputs in same form
Hi, I have a simple form as shown below where I am using your tagin module on the Pattern and Choices field. If I use the tagin class on a single field everything works fine, however, when I use the the tagin class on both input field, the Pattern field tags correctly but the Choices field disappears. Would be grateful if you can help me out. I have also attached the code below:
Form:
Script:
Output:
Get multiple values as an array
I have a problem, I'm trying to get an array with all the tags from the input text as values.
When i insert some values in the input i get this "123,2131,12" but i need to get something like this ["123","2131","12"], any solution?
Cannot install tagin.min.js from CDN
Hello, and first off thank you for the excellent little library here!
I've got a purely static site with no build, and am using Tagin straight from the CDN. It appears that I am able to use
tagin.js
just fine, but when I instead switch to the minified artifact,Tagin
is no longer defined:I'm not sure if this is just my lack of JS know-how showing or an issue w/ the minified JS, but I'd love any help figuring out how to make use of the minified library!
Allow for exporting tagin as a module
Solution:
Add
export default
to thetagin
function:Import the script as a module:
Then it is possible to import
tagin
inside a script:Reason:
This removes the current way of inline importing/using
tagin
inside HTML:Inline JS is generally considered bad practice.
Pros:
Easier to import a required function into an existing script file, if there will useful functions in the future.
Cons:
Internet Explorer does not support
import
. (Is that really a con though?)Dynamically rendered tagin-tag can't get value
Hi, thank you for this great work, it all worked fine and smooth. But I was trying to dynamically create tagin-tag with js, afterward, I want to retrieve the value out of the input element, but the value is null. But once I manually add some tags In the input field, I can get the input value afterward. Is there any suggestion or ways I can look up to?
dynacmically render.js
tagArr.forEach((el) => { const tagEl = document.createElement("span"); tagEl.setAttribute("class", "tagin-tag"); tagEl.innerHTML =
${el}; document.querySelector(".tagin-wrapper").appendChild(tagEl); });
getting value out of it, but it show null.
const tag = document.querySelector("#tag").value;
Thanks!
Add module export for using tagin via import
Unfortunately I couldn't get gulp to compile on my system so I didn't update the version in
dist
.But with this small change, I could easily
Refactor Tagin as a class
Allows to easily import the module and use functions such as
.addTag()
and.getTags()
outside the class itself. Also implements #10.This eliminates the ugly ways of adding and getting tags:
I updated the
README.md
to reflect the changes.Add recommendations
Hi there,
this isn't really an issue, rather a feature request. It would be very cool if Tagin would support recommendations or something similar like Datalists for Input fields
Value always null
I have a Laravel Livewire input
But when I click submit, the value was not recognized.
Any help is much appreciated.
Input field duplicated on dynamic field
Hello,
I have run into an issue where my tag field keeps getting duplicated on a dynamic input field.
This is what happens:
I am showing the input field on a modal whos content gets automatically populated depending on which element a user clicks on.
Here is the html for the modal, ignore the curly braces that's the Jinja template tags.
I update the fields on the modal with an
on click
from that list item.And here is a snippet of the modal
If I use the method shown in the example to update the tags field it works, but the old values stay there where I click on another list item (the rest of my fields update fine). I have been able to get it to update on every item, but the input field is then duplicated as shown in the screenshot.
Any ideas on how I can address this issue?