This JavaScript code provides a user-friendly tags input interface for entering and managing tags on a web page. It allows users to input tags separated by commas or by pressing Enter. The core functionality includes adding tags, displaying the remaining tag limit, and clearing all tags with a single click.
Furthermore, this code snippet simplifies the process of tagging content for better organization and presentation on your website.
Also Read: Bootstrap Tags input with Auto Complete
How to Create JavaScript Tags Input With Comma Separated
1. Start by including the necessary HTML structure in your web page. You can copy the following CDN links into your HTML file for icons. Ensure that you have the required JavaScript files linked in the <head>
section of your HTML.
<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script> <script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
2. The following HTML structure consists of an input field for adding tags, an icon button to trigger tag addition, a tag display area, and a “Clear All” button.
<main class='interface'> <div class='interface-top'> <input type='text' class='interface-input' placeholder='Press enter or add a comma after each tag'> <ion-icon class='interface-btn' name="pricetag-outline"></ion-icon> </div> <div class='interface-bottom'> <ul class='interface-list'></ul> <div class='interface-footer'> <p class='interface-remaining'>10 tags remaining</p> <button class='interface-clear'>clear all</button> </div> </div> </main>
3. You can customize the appearance of the tags and the input field by modifying the following CSS code to match your website’s style.
* { margin: 0; padding: 0; box-sizing: border-box; } body { height: 100vh; display: grid; place-items: center; } .interface *, ::placeholder { font: 100 0.75rem 'myriad pro',helvetica,sans-serif; } .interface { background: #fff; width: min(375px,90vw); border: 1.5px solid black; padding: 1rem; display: flex; flex-direction: column; gap: 1rem; } .interface-top { display: flex; gap: 0.75rem; } .interface-input { flex: 1; padding: 0.5rem 1rem; border: 1px solid rgba(0,0,0,0.9); outline: none; } .interface-input:focus { border-color: rgba(0,0,0,0.25); } ::placeholder { color: rgba(0,0,0,0.3); } .interface-btn { border: 1px solid rgba(0,0,0,0.9); font-size: 1rem; padding: 0.5rem; cursor: pointer; } .interface-btn:active { background-color: black; color: white; } .interface-bottom { display: flex; flex-direction: column; gap: 1rem; display: none; } .interface-list { list-style-type: none; display: flex; flex-wrap: wrap; gap: 0.75rem; border: 0.25px solid rgba(0,0,0,0.25); padding: 1rem; } .interface-tag { color: rgba(0,0,0,0.6); border: 0.5px solid rgba(0,0,0,0.75); padding: 0.35rem 0.75rem; display: flex; gap: 0.75rem; } .interface-close { cursor: pointer; color: rgba(0,0,0,0.65); } .interface-close:active { color: rgba(0,0,0,0.5); } .interface-footer { display: flex; align-items: center; justify-content: space-between; } .interface-remaining { margin-left: 1rem; } .interface-clear { color: white; background-color: black; border: 1.5px solid black; outline: none; padding: 0.5rem 1rem; cursor: pointer; } .interface-clear:active { border: 1.5px solid black; background-color: transparent; color: black; } .active { display: flex; }
4. The following JavaScript code provides the necessary functionality for adding and managing tags. Add this code to your project:
const input = document.querySelector('.interface-input'); const bottom = document.querySelector('.interface-bottom'); const list = document.querySelector('.interface-list'); const remaining = document.querySelector('.interface-remaining'); const limit = 10; const totalTags = () => list.querySelectorAll('.interface-tag').length; function overLimit() { input.value = ''; input.placeholder = totalTags() >= limit ? `You've reached the limit of 10 tags` : `Press enter or add a comma after each tag`; } function closeTag(e) { e.target.parentElement.remove(); remaining.textContent = `${limit - totalTags()} tags remaining` totalTags() === 0 && clearTags(); overLimit(); } function clearTags() { list.innerHTML = ''; input.value = ''; remaining.textContent = '10 tags remaining'; setTimeout(() => bottom.classList.remove('active'),500); } function showTags() { if (!input.value.length) return; bottom.classList.add('active'); list.innerHTML += createMarkup(); input.value = ''; remaining.textContent = `${limit - totalTags()} tags remaining`; } const isEnter = (e) => e.keyCode === 13 && showTags(); function createMarkup() { const tags = input.value.split(/[, ]/); const markup = []; if (totalTags() >= limit || !tags[0].length) overLimit(); for (let i = 0; i < limit - totalTags(); i++) { if (!tags[i]) continue; markup.push(` <li class='interface-tag'> <span class='interface-tagName'>${tags[i]}</span> <span class='interface-close'>x</span> </li> `); } return markup.join(''); } function init(e) { e.target.matches('.interface-clear') && clearTags(); e.target.matches('.interface-close') && closeTag(e); e.target.matches('.interface-btn') && showTags(); } input.addEventListener('input',createMarkup,false); input.addEventListener('keydown',isEnter,false); document.addEventListener('click',init,false);
That’s all! hopefully, you have successfully created Javascript Tags Input With Comma Separated. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.