-
Notifications
You must be signed in to change notification settings - Fork 76
/
jquery.simplyCountable.js
60 lines (48 loc) · 1.85 KB
/
jquery.simplyCountable.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* jQuery Simply Countable plugin
* Provides a character counter for any text input or textarea
*
* @version 0.2
* @homepage http://github.com/aaronrussell/jquery-simply-countable/
* @author Aaron Russell (http://www.aaronrussell.co.uk)
*
* Copyright (c) 2009 Aaron Russell (aaron@gc4.co.uk)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function($){
$.fn.simplyCountable = function(options){
options = $.extend({
counter: '#counter',
countType: 'characters',
maxCount: 140,
countDirection: 'down',
safeClass: 'safe',
overClass: 'over'
}, options);
var countable = this;
var countCheck = function(){
if (options.countType === 'words'){
var count = options.maxCount - countable.val().split(/[\s]+/).length;
if (countable.val() === '') count += 1;
}
else var count = options.maxCount - countable.val().length;
if (!$(options.counter).hasClass(options.safeClass) && !$(options.counter).hasClass(options.overClass)){
if (count < 0) $(options.counter).addClass(options.overClass);
else $(options.counter).addClass(options.safeClass);
}
else if (count < 0 && $(options.counter).hasClass(options.safeClass)){
$(options.counter).removeClass(options.safeClass).addClass(options.overClass);
}
else if (count >= 0 && $(options.counter).hasClass(options.overClass)){
$(options.counter).removeClass(options.overClass).addClass(options.safeClass);
}
if (options.countDirection === 'up'){
count = count - (count*2) + options.maxCount;
}
$(options.counter).text(count);
};
countCheck();
countable.keyup(countCheck);
};
})(jQuery);