Skip to content

Commit bf32a6a

Browse files
committed
Flexible counterText/countDownText replacements
1 parent bb7bd13 commit bf32a6a

File tree

6 files changed

+10
-15
lines changed

6 files changed

+10
-15
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ Define minimum characters and set custom `countDownText` ([view editable code](h
5353
```javascript
5454
$('input').textcounter({
5555
min: 20,
56-
countDownText: "Characters Left: "
5756
});
5857
```
5958

@@ -117,15 +116,15 @@ countContainerClass : "text-count-wrapper", // class applied to the count
117116
textCountClass : "text-count", // class applied to the counter length
118117
inputErrorClass : "error", // error class appended to the input element if error occurs
119118
counterErrorClass : "error", // error class appended to the countContainerElement if error occurs
120-
counterText : "Total Count: ", // counter text
119+
counterText : "Total Count: %d", // counter text, %d replaced with count value
121120
errorTextElement : "div", // error text element
122121
minimumErrorText : "Minimum not met", // error message for minimum not met,
123122
maximumErrorText : "Maximum exceeded", // error message for maximum range exceeded,
124123
displayErrorText : true, // display error text messages for minimum/maximum values
125124
stopInputAtMaximum : true, // stop further text input if maximum reached
126125
countSpaces : false, // count spaces as character (only for "character" type)
127126
countDown : false, // if the counter should deduct from maximum characters/words rather than counting up
128-
countDownText : "Remaining: ", // count down text
127+
countDownText : "Remaining: %d", // count down text, %d replaced with remaining value
129128
countExtendedCharacters : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters)
130129

131130
// Callback API

bower.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "jquery-text-counter",
3-
"version": "0.4.1",
43
"main": "textcounter.js",
54
"license": "MIT",
65
"ignore": [

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"minimum",
1212
"maximum"
1313
],
14-
"version": "0.4.1",
1514
"author": {
1615
"name": "ractoon",
1716
"url": "http://www.ractoon.com"

textcounter.jquery.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"minimum",
1010
"maximum"
1111
],
12-
"version": "0.4.1",
1312
"author": {
1413
"name": "ractoon",
1514
"url": "http://www.ractoon.com"

textcounter.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*!
2-
* jQuery Text Counter Plugin v0.4.1
32
* https://github.com/ractoon/jQuery-Text-Counter
43
*
54
* Copyright 2014 ractoon
@@ -23,10 +22,11 @@
2322

2423
// append the count element
2524
var counterText = base.options.countDown ? base.options.countDownText : base.options.counterText,
26-
counterNum = base.options.countDown ? base.options.max : 0;
25+
counterNum = base.options.countDown ? base.options.max : 0,
26+
$formatted_counter_text = $('<div/>').html(counterText.replace('%d', '<span class="' + base.options.textCountClass + '">' + counterNum + '</span>')).contents();
2727

28-
base.$text_counter = $('<span />').addClass(base.options.textCountClass).text(counterNum);
29-
base.$container = $('<' + base.options.countContainerElement + '/>').addClass(base.options.countContainerClass).text(counterText).append(base.$text_counter);
28+
base.$container = $('<' + base.options.countContainerElement + '/>').addClass(base.options.countContainerClass).append($formatted_counter_text);
29+
base.$text_counter = base.$container.find('span');
3030
base.$el.after(base.$container);
3131

3232
// bind input events
@@ -86,7 +86,6 @@
8686
// set the current text count
8787
base.setCount(textTotalCount);
8888

89-
9089
if (base.options.min > 0 && eventTriggered) { // if a minimum value has been set
9190
if (textCount < base.options.min) {
9291
base.setErrors('min');
@@ -211,15 +210,15 @@
211210
'textCountClass' : "text-count", // class applied to the counter length
212211
'inputErrorClass' : "error", // error class appended to the input element if error occurs
213212
'counterErrorClass' : "error", // error class appended to the countContainerElement if error occurs
214-
'counterText' : "Total Count: ", // counter text
213+
'counterText' : "Total Count: %d", // counter text
215214
'errorTextElement' : "div", // error text element
216215
'minimumErrorText' : "Minimum not met", // error message for minimum not met,
217216
'maximumErrorText' : "Maximum exceeded", // error message for maximum range exceeded,
218217
'displayErrorText' : true, // display error text messages for minimum/maximum values
219218
'stopInputAtMaximum' : true, // stop further text input if maximum reached
220219
'countSpaces' : false, // count spaces as character (only for "character" type)
221220
'countDown' : false, // if the counter should deduct from maximum characters/words rather than counting up
222-
'countDownText' : "Remaining: ", // count down text
221+
'countDownText' : "Remaining: %d", // count down text
223222
'countExtendedCharacters' : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters)
224223

225224
// Callback API

textcounter.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)