forked from gueamu/jquery-tumblr-tags-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumblertags.js
43 lines (37 loc) · 1.26 KB
/
tumblertags.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* jquery.tumblertags 0.1. tumbler inspired tag editor
*
* Copyright (c) 2009 Christof Haemmerle ([email protected])
*
* Released under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* >> Basically you can do anything you want but leave this header as is <<
*
*/
(function ($) {
$.fn.tumblertags = function () {
return this.each(function () {
var $tabwrapper = $(this);
var $inputField = $tabwrapper.find('input')
// conlick focus input field
$tabwrapper.click(function(){
$inputField.focus();
});
// when pressing the returnkey, wraping the text of input field in a span and prepend it to the input field.
$inputField.keypress(function(e, keyCode){
keyCode = keyCode || e.keyCode;
if (keyCode == 8 && $inputField.val() == '') {
$tabwrapper.find('span:last').remove();
};
if (keyCode == 13 && $inputField.val() != '') {
$newTag = $('<span class="tag">' + $inputField.val() + '<a href="#" title="remove tag">x</a></span>');
$inputField.val('');
$newTag.insertBefore($inputField).find('a').click(function() {
$(this).parent().remove();
});
};
});
});
};
})(jQuery);