body {
padding:0;
margin:0;
font-family: 'Raleway', sans-serif;
background: #e9e9e9;
}
#app {
padding: 20px;
width: 600px;
height: auto;
background: #fff;
border-radius: 4px;
box-shadow: 0 40px 50px rgba(0,0,0,0.25);
outline: none;
font-size: 1em;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
position: absolute;
& input {
outline: none;
width: auto;
border: 0;
float: left;
padding: 8px;
background: none;
}
&::before {
content: '';
display: table;
}
&::after {
content: '';
display: table;
clear: both;
}
}
.tag {
border-radius: 3px;
background: #b5aff9;
float: left;
margin: 3px;
padding: 4px;
font-size: 1em;
vertical-align: middle;
box-shadow: 0px 1px 4px #c6c6c6,
0px 2px 17px #d1d1d1;
& a {
color: #000;
padding-right: 10px;
padding-left:5px;
padding-top: 5px;
padding-bottom: 5px;
margin-right: 5px;
}
& span {
padding-right: 10px;
padding-left: 0px;
padding-top: 5px;
padding-bottom: 5px;
}
}
.note {
position: fixed;
bottom: 10px;
text-align: center;
width: 100%;
}
View Compiled
$(document).ready(function(){
var $input = $("#app input"),
$appendHere = $(".tagHere"),
oldKey = 0, newKey,
TABKEY = 9;
$input.focus();
$input.keydown(function(e){
if(e.keyCode == TABKEY) {
if(e.preventDefault) {
e.preventDefault();
if($(this).val() == '' || $(this).val() == ' ') {
return false;
}
addTag($(this));
}
return false;
}
if($(this).val() == '' && e.keyCode === 8) {
$(".tag:last-child").remove();
}
})
function addTag(element) {
var $tag = $("<div />"), $a = $("<a href='#' />"), $span = $("<span />");
$tag.addClass('tag');
$('<i class="fa fa-times" aria-hidden="true"></i>').appendTo($a);
$span.text($(element).val());
$a.bind('click', function(){
$(this).parent().remove();
$(this).unbind('click');
});
$a.appendTo($tag);
$span.appendTo($tag);
$tag.appendTo($appendHere);
$(element).val('');
}
});