Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Fires when counter is under min limit.
```javascript
type : "character", // "character" or "word"
min : 0, // minimum number of characters/words
max : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute
max : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute, , 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr")
autoCustomAttr : "counterlimit", // custom attribute name with the counter limit if the max is 'autocustom'
countContainerElement : "div", // HTML element to wrap the text count in
countContainerClass : "text-count-wrapper", // class applied to the countContainerElement
textCountMessageClass : "text-count-message", // class applied to the counter message
Expand All @@ -136,8 +137,8 @@ minDisplayCutoff : -1, // maximum number
maxDisplayCutoff : -1, // maximum number of characters/words below the maximum to display a count

// Callback API
maxunder : function(el){}, // Callback: function(element) - Fires when counter under max limit
minunder : function(el){}, // Callback: function(element) - Fires when counter under min limit
maxunder : function(el){}, // Callback: function(element) - Fires when counter is under max limit
minunder : function(el){}, // Callback: function(element) - Fires when counter is under min limit
maxcount : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count
mincount : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count
init : function(el){} // Callback: function(element) - Fires after the counter is initially setup
Expand Down Expand Up @@ -166,4 +167,5 @@ init : function(el){} // Callback: func
- [dtipson](https://github.com/dtipson) - multiple classes error fix
- [jmichalicek](https://github.com/jmichalicek) - count carriage returns/newlines as 2 characters
- [diptopol](https://github.com/diptopol) - `stopInputAtMaximum` with `twoCharCarriageReturn` count fix, trimmed newline calculation fix, maximum text reached condition fix, text count overflow notification
- [trevorloflin](https://github.com/trevorloflin) - `minDisplayCutoff` and `maxDisplayCutoff` options
- [trevorloflin](https://github.com/trevorloflin) - `minDisplayCutoff` and `maxDisplayCutoff` options
- [t3mujin](https://github.com/t3mujin) - autocustom support (maxlength workaround), text fixes
35 changes: 23 additions & 12 deletions textcounter.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
base.$container.text('error: [maxlength] attribute not set');
}
}
else if (base.options.max == 'autocustom') {
var max = base.$el.attr(base.options.autoCustomAttr);

if (typeof max !== 'undefined' && max !== false) {
base.options.max = max;
}
else {
base.$container.text('error: [' + base.options.autoCustomAttr + '] attribute not set');
}
}

// if this is a countdown counter deduct from the max characters/words
textTotalCount = base.options.countDown ? base.options.max - textCount : textCount;
Expand Down Expand Up @@ -151,17 +161,17 @@
base.clearErrors('max');
}
}
// hide the counter if it doesn't meet either the minimum or maximum display cutoff
if (base.options.minDisplayCutoff == -1 && base.options.maxDisplayCutoff == -1) {
base.$container.show();
} else if (textCount <= base.options.min + base.options.minDisplayCutoff) {
base.$container.show();
} else if (base.options.max !== -1 && textCount >= base.options.max - base.options.maxDisplayCutoff) {
base.$container.show();
} else {
base.$container.hide();
}
// hide the counter if it doesn't meet either the minimum or maximum display cutoff
if (base.options.minDisplayCutoff == -1 && base.options.maxDisplayCutoff == -1) {
base.$container.show();
} else if (textCount <= base.options.min + base.options.minDisplayCutoff) {
base.$container.show();
} else if (base.options.max !== -1 && textCount >= base.options.max - base.options.maxDisplayCutoff) {
base.$container.show();
} else {
base.$container.hide();
}
};

base.textCount = function(text) {
Expand Down Expand Up @@ -305,7 +315,8 @@
$.textcounter.defaultOptions = {
'type' : "character", // "character" or "word"
'min' : 0, // minimum number of characters/words
'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute
'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute, 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr")
'autoCustomAttr' : "counterlimit", // custom attribute name with the counter limit if the max is 'autocustom'
'countContainerElement' : "div", // HTML element to wrap the text count in
'countContainerClass' : "text-count-wrapper", // class applied to the countContainerElement
'textCountMessageClass' : "text-count-message", // class applied to the counter message
Expand Down
16 changes: 8 additions & 8 deletions textcounter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.