LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Input Tags With Automatically Comma Separated Formatting Using JavaScript

Input Tags With Automatically Comma Separated Formatting Using JavaScript

Input-Tags-With-Automatically-Comma-Separated-Formatting-Using-JavaScript
The “simple-tags” is a modest Vanilla JavaScript plugin that allows you to create tag input. This pure JS tags input plugin is useful to insert new tags, remove tags and display predefined tag in an HTML container. Besides this, you can also style the tags field with additional CSS according to your needs.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add JavaScript Audio Waveform Visualizer Player With Pure Vanilla JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

<style type="text/css">
.simple-tags {
border: 1px solid #1e87f0;
box-sizing: border-box;
margin: 1em;
padding: 0em 0.5em;
display: -webkit-box;
display: flex;
flex-wrap: wrap;
font-size: 14px;
border-radius: 5px; }
.simple-tags > ul {
list-style: none;
padding: 0;
margin: 0;
display: -webkit-box;
display: flex;
flex-wrap: wrap; }
.simple-tags ul li {
margin: 0.5em 0.2em;
padding: 0.5em;
color: #fff;
background-color: #1e87f0;
border-radius: 5px; }
.simple-tags ul li a {
margin: 0.5em 0.2em;
text-decoration: none;
color: inherit; }
.simple-tags input {
padding: 0.9em 0.5em;
box-sizing: border-box;
-webkit-box-flex: 1;
flex-grow: 1;
border: none;
outline: none;
font-size: inherit;
font-family: inherit;
color: inherit; }
</style>

HTML:

<div class="simple-tags" id="container" data-simple-tags="CodeHim, HTML, CSS, JavaScript, jQuery, Bootstrap"></div>

JavaScript:

<script type="text/javascript">
function Tags(element) {
var DOMParent = element;
var DOMList;
var DOMInput;
var dataAttribute;
var arrayOfList;

function DOMCreate() {
var ul = document.createElement('ul');
var li = document.createElement('li');
var input = document.createElement('input');
DOMParent.appendChild(ul);
DOMParent.appendChild(input); // first child is <ul>

DOMList = DOMParent.firstElementChild; // last child is <input>

DOMInput = DOMParent.lastElementChild;
}

function DOMRender() {
// clear the entire <li> inside <ul> 
DOMList.innerHTML = ''; // render each <li> to <ul>

arrayOfList.forEach(function (currentValue, index) {
var li = document.createElement('li');
li.innerHTML = "".concat(currentValue, " <a>&times;</a>");
li.querySelector('a').addEventListener('click', function () {
if (confirm('Continue to remove tag?')) {
onDelete(index);
}

return false;
});
DOMList.appendChild(li);
setAttribute();
});
}

function onKeyUp() {
DOMInput.addEventListener('keyup', function (event) {
var text = this.value.trim(); // check if ',' or 'enter' key was press

if (text.includes(',') || event.keyCode == 13) {
// check if empty text when ',' is remove
if (text.replace(',', '') != '') {
// push to array and remove ','
arrayOfList.push(text.replace(',', ''));
} // clear input


this.value = '';
}

DOMRender();
});
}

function onDelete(id) {
arrayOfList = arrayOfList.filter(function (currentValue, index) {
if (index == id) {
return false;
}

return currentValue;
});
DOMRender();
}

function getAttribute() {
dataAttribute = DOMParent.getAttribute('data-simple-tags');
dataAttribute = dataAttribute.split(','); // store array of data attribute in arrayOfList

arrayOfList = dataAttribute.map(function (currentValue) {
return currentValue.trim();
});
}

function setAttribute() {
DOMParent.setAttribute('data-simple-tags', arrayOfList.toString());
}

getAttribute();
DOMCreate();
DOMRender();
onKeyUp();
} // run immediately


(function () {
var DOMSimpleTags = document.querySelectorAll('.simple-tags');
DOMSimpleTags = Array.from(DOMSimpleTags);
DOMSimpleTags.forEach(function (currentValue, index) {
// create Tags
new Tags(currentValue);
});
})();



// Use It On Each Desired DIV
var el = document.getElementById('container');
new Tags(el);
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
7 HTML/CSS Widgets To Give Your Site A Distinct Outlook

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *