-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathacfl.js
67 lines (55 loc) · 1.68 KB
/
acfl.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
61
62
63
64
65
66
67
/*
* Text Limit jQuery Plugin
* Author: Vinicius Massuchetto
* URL: http://vinicius.soylocoporti.org.br/text-limit-jquery-plugin
*/
(function($){
var acfl;
function colorize (obj, length) {
countbox = $(obj);
threshold1 = length / 2;
threshold2 = length / 4;
n = parseInt(countbox.html(), 10);
if (n < threshold2) {
countbox.removeClass('count-box-threshold1');
countbox.addClass('count-box-threshold2');
} else if (n < threshold1) {
countbox.addClass('count-box-threshold1');
} else if (countbox.hasClass('count-box-threshold1') || countbox.hasClass('count-box-threshold2')) {
countbox.removeClass('count-box-threshold1');
countbox.removeClass('count-box-threshold2');
}
}
$.fn.textlimit = function (length) {
this.css('padding-right', '40px');
countbox = $('<span>');
countbox.addClass('count-box');
countbox.addClass(countclass = this.selector.replace(/[^0-9A-Za-z]/, '').replace(' ', '-') + '-count');
var n = 0;
if (this.is('input')) {
n = this.val().length;
} else if (this.is('textarea')) {
n = this.text().length;
}
n = length - n;
countbox.html(n);
this.parent().append(countbox);
colorize(countbox, length);
this.keyup(function(){
obj = $(this);
n = obj.val().length;
if (n > length)
obj.val(obj.val().substring(0, length));
else {
countbox = obj.parent().children('.count-box');
countbox.html(length - n);
colorize(countbox, length);
}
});
};
})(jQuery);
jQuery(document).ready(function(){
for (var f in acfl) {
jQuery(f).textlimit(acfl[f]);
}
});