keyword input field
add/remove keywords to a field
HTML
<div id="box">
<ul>
<li><input type="text" id="type"/></li>
</ul>
</div>
<small>*put something in the input above, and press space.</small>
CSS
#box{padding:8px 2px;border:1px solid #CCCCCC;display:block}
#box input,#box input:active,#box input:focus{border:none;background:none;outline:none}
#box ul,#box li,#box input{margin:0;padding:0}
#box ul,#box li{list-style:none}
#box li,#box a{display:inline-block;margin:2px}
#box span{background-color:#EDEDED;padding:3px;color:#666666;border:1px solid #CCCCCC}
#box a{font-size:12px;line-height:12px;color:#666666;text-decoration:none;padding:1px 3px;margin-left:5px;font-weight:bold;background-color:#FFFFFF;vertical-align:top;border:1px solid #CCCCCC;margin-top:-1px}
#box a:hover{background-color:#666666;color:#FFFFFF}
/* why not? */
body{font-family:sans-serif}
#box span{border-radius:5px}
#box a{border-radius:100%;overflow:hidden}
JavaScript
function closer() {
$('#box a').on('click', function() {
$(this).parent().parent().remove();
console.log('remove disabled');
if ($('#box ul li').length <= 5) {
$('#type').prop('disabled', false);
}
});
}
$('#type').on('input', function(e) {
var tx = $(this).val();
var array = tx.split(' ');
var limit = 5;
var remainder = '';
if (1 < array.length) { //change to 32 for spacebar instead of enter
if ($('#box ul li').length > limit) {
$(this).val('');
console.log('add disabled');
$('#type').prop('disabled', 'disabled');
} else {
$.each(array, function(i, text) {
if(i < limit && i === array.length - 1 && text) { //no space at the end
remainder = text;
return false;
} else if($('#box ul li').length <= limit) {
if (text) {
$('#type').parent().before('<li><span>' + text + '<a href="javascript:void(0);">x</a></span></li>');
closer();
};
} else {
$('#type').val('');
console.log('add disabled');
$('#type').prop('disabled', 'disabled');
remainder = '';
return false;
};
})
$('#type').val(remainder);
}
}
});