Skip to content

Instantly share code, notes, and snippets.

@iEverX
Created December 19, 2012 07:54
Show Gist options
  • Save iEverX/4335136 to your computer and use it in GitHub Desktop.
Save iEverX/4335136 to your computer and use it in GitHub Desktop.
Add tags. Need jQuery
#input_outer {
border: 1px solid orange;
border-radius: 3px;
padding: 2px; display:
inline; width: 200px;
overflow: hidden;
}
.t_input {
width: 100px;
border: none;
outline: none;
}
.tag {
background-color: rgba(102, 195, 242, 0.5);
padding: 0 5px;
margin-right:
3px;
}
<html>
<head>
<title>Tag INPUT</title>
<link rel="stylesheet" href="tag.css">
<script type="text/javascript" src="../jquery-1.8.3.js"></script>
</head>
<body>
<div id="input_outer" node-type="container">
<input type="text" class="t_input" id="now_input">
</div>
<script type="text/javascript" src="./tag.js"></script>
</body>
</html>
$(function (){
$('#input_outer').focus(focusOnNowInput).click(focusOnNowInput);
$('#now_input').keydown(function(event) {
var keycode = event.keyCode;
if (event.keyCode == 32) {
if ($.trim(this.value) != '') {
$('<span class="tag"></span>').html($.trim(this.value)).insertBefore(this);
this.value = '';
}
return false;
}
else if (event.keyCode == 8) {
if ($.trim(this.value) == '') {
$('.tag').last().remove();
}
}
});
});
function focusOnNowInput() {
$('#now_input')[0].focus();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment