Simple Closable Tags in JavaScript

Simple Closable Tags in JavaScript
Code Snippet:Tags with hover effect
Author: Yannick Bisaillon
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 2,108
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet helps you to create simple closable tags with icons and hover effects. It uses Material Design icons and  CSS animation keyframes to build attractive tags. You can integrate this code snippet to create tags input or tags cloud.

How to Create Simple Closable Tags in JavaScript

1. First of all, create the HTML structure as follows:

  <p style="margin: 15px 0"> Click the cross icon to close the tag.</p>
  <div class="demo">
	<div class="token token--icon">
		<i class="material-icons icon-left">attach_money</i>
		Ragnar
		<i class="material-icons icon-clear">clear</i>
	</div>

	<div class="token token--icon">
		<i class="material-icons icon-left">attach_money</i>
		$20-100
		<i class="material-icons icon-clear">clear</i>
	</div>

	<div class="token token--icon">
		<i class="material-icons icon-left">timer</i>
		5-10s
		<i class="material-icons icon-clear">clear</i>
	</div>

	<div class="token">
		Tag
		<i class="material-icons icon-clear">clear</i>
	</div>

	<div class="token token--blue">
		Tag blue
		<i class="material-icons icon-clear">clear</i>
	</div>

	<div class="token token--pink">
		Tag pink
		<i class="material-icons icon-clear">clear</i>
	</div>
</div>

2. After that, add the following CSS styles to your project:

.demo .token {
  margin-right: 5px;
}

.token {
  position: relative;
  display: inline-block;
  padding-left: 12px;
  padding-right: 12px;
  height: 32px;
  line-height: 32px;
  background-color: #e0e0e0;
  font-size: 13px;
  font-weight: 500;
  color: rgba(66, 66, 66, 0.7);
  border-radius: 16px;
  border-bottom: 2px solid lightgray;
  overflow: hidden;
  z-index: 1;
}
.token:after {
  position: absolute;
  left: 0;
  top: 100%;
  content: "";
  width: 100%;
  height: 36px;
  background-color: lightgray;
  z-index: -1;
  transition: transform 0.3s;
}
.token:hover:after {
  transform: translateY(-100%);
  transition: transform 0.3s;
}
.token--blue {
  background-color: #0277bd;
  border-color: #0267a4;
  color: white;
}
.token--blue:after {
  background-color: #0267a4;
}
.token--pink {
  background-color: #f50057;
  border-color: #dc004e;
  color: white;
}
.token--pink:after {
  background-color: #dc004e;
}
.token--icon {
  padding-left: 43px;
}
.token.is-close {
  display: none;
}
.token i {
  font-size: 16px;
  line-height: 32px;
}
.token .icon-clear {
  float: right;
  padding-left: 8px;
  cursor: pointer;
}
.token .icon-left {
  position: absolute;
  left: 0;
  color: white;
  border-radius: 16px;
  background-color: #212121;
  padding-left: 9px;
  padding-right: 9px;
}
.token .icon-left:hover {
  -webkit-animation: ease spin 0.5s;
          animation: ease spin 0.5s;
}
.token img {
  float: left;
  margin: 0 8px 0 -12px;
  height: 32px;
  width: 32px;
  border-radius: 16px;
}
.token img:hover {
  -webkit-animation: ease spin 0.5s;
          animation: ease spin 0.5s;
}

@-webkit-keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes fade-out {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
    display: none;
  }
}
@keyframes fade-out {
  0% {
    opacity: 1;
    visibility: visible;
  }
  100% {
    opacity: 0;
    visibility: hidden;
    display: none;
  }
}

3. Finally, add the following JavaScript code and done.

(function() {
  'use strict';
  document.addEventListener("DOMContentLoaded", function(event) {
    var close = this.querySelectorAll('.icon-clear'); 

    for (var i = 0; i < close.length; i++) {
      close[i].addEventListener('click', function(event) {
        this.parentNode.classList.add('is-close');
      });
    }
		
  });
}());

That’s all! hopefully, you have successfully integrated this closable tags code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X