Simple tag input for Bootstrap
Install tagin with npm:
npm install tagin
Install from cdn:
<link rel="stylesheet" href="https://unpkg.com/tagin@2.0.2/dist/tagin.min.css">
<script src="https://unpkg.com/tagin@2.0.2/dist/tagin.min.js"></script>
Place css
between <head></head>
tags:
<head>
<link rel="stylesheet" href="https://unpkg.com/tagin@2.0.2/dist/tagin.min.css">
</head>
Place js
before </body>
tag:
<body>
...
<script src="https://unpkg.com/tagin@2.0.2/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):<input type="text" name="tags" class="form-control tagin" value="red,green,blue">
<script>
new Tagin(document.querySelector('.tagin'))
</script>
<!-- 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: -->
<script>
const tagin = new Tagin(document.querySelector('.tagin'), {
placeholder: 'Add a color... (then press comma)'
})
</script>
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: -->
<script>
const tagin = new Tagin(document.querySelector('.tagin'), {
separator: ' '
})
</script>
<!-- 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: -->
<script>
const tagin = new Tagin(document.querySelector('.tagin'), {
duplicate: true
})
</script>
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: -->
<script>
const tagin = new Tagin(document.querySelector('.tagin'), {
transform: 'input => input.toUpperCase()'
})
</script>
<!-- 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-tagin-placeholder="Add a color... (then press comma or enter)">
<!-- Or using option: -->
<script>
const tagin = new Tagin(document.querySelector('.tagin'), {
enter: true
})
</script>
Tagin provides addTag
method to add tags programmatically.
<input type="text" name="tags" class="form-control tagin" value="red,green,blue">
<button class="btn btn-secondary btn-sm" id="add1" type="button">Add yellow</button>
<button class="btn btn-secondary btn-sm" id="add2" type="button">Add cyan & black</button>
<script>
const tagin = new Tagin(document.querySelector('.tagin'))
document.getElementById('add1').addEventListener('click', function () {
tagin.addTag('yellow')
this.remove()
})
document.getElementById('add2').addEventListener('click', function () {
tagin.addTag(['cyan', 'black'])
this.remove()
})
</script>
To get the value, Tagin provides the getTag
method to get the string value with its separator, and the getTags
method to get the value as an array.
<input type="text" name="tags" class="form-control tagin" value="red,green,blue">
<button class="btn btn-outline-secondary mt-4 btn-sm" id="get" type="button">Get the value</button>
<script>
const tagin = new Tagin(document.querySelector('.tagin'))
document.getElementById('get').addEventListener('click', function () {
console.log(tagin.getTag()) // red,green,blue
console.log(tagin.getTags()) // ["red","green","blue"]
})
</script>
<form class="needs-validation" novalidate>
<input type="text" name="tags" class="form-control tagin" required>
<div class="valid-feedback">Ok</div>
<div class="invalid-feedback">Required</div>
<button class="btn btn-primary mt-3" type="submit">Submit</button>
</form>
<script>
new Tagin(document.querySelector('.tagin'))
// Example starter JavaScript for disabling form submissions if there are invalid fields
void (function () {
document.querySelectorAll('.needs-validation').forEach(form => {
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
})
})
})()
</script>
Name | Type | Default | Description |
---|---|---|---|
separator |
string | , |
Separator between tags |
placeholder |
string | Short hint in the input field before the user enters a value | |
duplicate |
boolean | false |
Allow duplicates |
transform |
string | input => input |
Function to change value. The function must be an arrow function |
enter |
boolean | false |
Force adding tag when enter key is pressed |
Name | Params | Returns | Example |
---|---|---|---|
addTag |
string | array |
void |
|
getTag |
null |
string |
|
getTags |
null |
array |
|