Press "Enter" to skip to content

Beautiful Draggable Tag Box Plugin with jQuery | Tag Me

Tag Me is a simple jQuery script to turn a plain text area into a beautiful animated tag input box that allows you to:

  • Press Enter to add tags.
  • Double click to edit the tag.
  • Click on x or Double Backspace or remove all text and hit Enter to delete a tag.
  • Sort tags using jQuery UI draggable.

jquery tag library, selectize jquery plugin example, javascript tag input, tags field, multiple tags in input field, tag it jquery

Nice Simple Tag Input With Minimal Design For Bootstrap | MAB.Bootstrap.TagInput

How to make use of it:

1. Load the necessary jQuery library, JavaScript, and CSS for jQuery UI into the document.

<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>

2. Create a text area containing a list of predefined tags separated by a space.

<textarea class="tagarea">
  Codepen Twitter Facebook Vini TagMe webcodeflow
</textarea>

3. Basic CSS for designing the tag input box.

.tagarea { display: none; }

.tag-box {
  list-style: none;
  padding: 3px;
  margin: 0;
  display: inline-block;
  font-family: arial;
  width: 100%;
  border: 1px solid #F44336;
  border-radius: 4px;
}

.tag-box * {
  -webkit-transition-property: all;
  transition-property: all;
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
}

.tag-box li {
  padding: 4px 6px;
  float: left;
  display: inline-block;
}

.tag-box li.tags {
  background: #2196F3;
  color: #fff;
  border-radius: 4px;
  margin: 4px 3px;
  position: relative;
}

.tag-box li.tags .close {
  display: inline-block;
  position: absolute;
  color: #BBDEFB;
  overflow: hidden;
  width: 0;
  right: 0;
  height: 18px;
  cursor: pointer;
  top: 4px;
}

.tag-box li.tags .close:after { content: 'x'; }

.tag-box li.tags.edit {
  background: none;
  border: 1px solid #2196F3;
  color: #2196F3;
}

.tag-box li.tags.edit .input-tag { height: auto; }

.tag-box li.tags.edit:hover { padding: 4px 6px; }

.tag-box li.tags.danger {
  background: #F00;
  opacity: 0.8;
}

.tag-box li.tags:hover { padding-right: 18px; }

.tag-box li.tags:hover .close { width: 14px; }

.tag-box li .input-tag {
  color: #fff;
  height: 24px;
  vertical-align: middle;
  border: none;
  outline: none;
  background: none;
}

.tag-box li .input-tag:hover,
.tag-box li .input-tag:focus {
  outline: none;
  border: none;
}

4. JavaScript (jQuery) Activates the draggable tag input box.

var backSpace;
var close = '<a class="close"></a>'; 
var PreTags = $('.tagarea').val().trim().split(" ");

$('.tagarea').after('<ul class="tag-box"></ul>');

for (i=0 ; i < PreTags.length; i++ ){
  $('.tag-box').append('<li class="tags">'+PreTags[i]+close+'</li>');
}

$('.tag-box').append('<li class="new-tag"><input class="input-tag" type="text"></li>');

// Taging 
$('.input-tag').bind("keydown", function (kp) {
  var tag = $('.input-tag').val().trim();
  $(".tags").removeClass("danger");
    if(tag.length > 0){
      backSpace = 0;
      if(kp.keyCode  == 13){
        $(".new-tag").before('<li class="tags">'+tag+close+'</li>');
         $(this).val('');
    }}
    
    else {if(kp.keyCode == 8 ){
     $(".new-tag").prev().addClass("danger");
     backSpace++;
     if(backSpace == 2 ){
       $(".new-tag").prev().remove(); 
       backSpace = 0;
     }
    }
 }
});

//Delete tag
$(".tag-box").on("click", ".close", function()  {
  $(this).parent().remove();
});

$(".tag-box").click(function(){
  $('.input-tag').focus();
});

// Edit
$('.tag-box').on("dblclick" , ".tags", function(cl){
  var tags = $(this); 
  var tag = tags.text().trim();
  $('.tags').removeClass('edit');
  tags.addClass('edit');
  tags.html('<input class="input-tag" value="'+tag+'" type="text">')
  $(".new-tag").hide();
  tags.find('.input-tag').focus();
  
  tag = $(this).find('.input-tag').val() ;
  $('.tags').dblclick(function(){ 
    tags.html(tag + close);
    $('.tags').removeClass('edit');
    $(".new-tag").show();
  });
  
  tags.find('.input-tag').bind("keydown", function (edit) {
    tag = $(this).val() ;
    if(edit.keyCode  == 13){
      $(".new-tag").show();
      $('.input-tag').focus();
      $('.tags').removeClass('edit');
      if(tag.length > 0){
        tags.html(tag + close);
      }
      else{
        tags.remove();
      }
  }
});  
});

// sorting
$(function() {
$( ".tag-box" ).sortable({
items: "li:not(.new-tag)",
  containment: "parent",
  scrollSpeed: 100
});

$( ".tag-box" ).disableSelection();

});

Pretty Draggable Tag Box, Tag Me Plugin/Github


See Demo And Download

Official Website(vineethtrv): Click Here

This superior jQuery/javascript plugin is developed by vineethtrv. For extra advanced usage, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published.