Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for per-field limits #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions jquery.charactercounter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Character Counter v1.5.1
* Character Counter v1.5.2
* ======================
*
* Character Counter is a simple, Twitter style character counter.
Expand Down Expand Up @@ -69,16 +69,16 @@
delete options.customFields['class'];
}

return '<'+ options.counterWrapper +customFields(options.customFields)+' class="' + classString + '"></'+ options.counterWrapper +'>';
return '<'+ options.counterWrapper +customFields(options.customFields)+' class="' + classString + '"' + ' data-limit="' + options.limit + '"' + '></'+ options.counterWrapper +'>';
}

function renderText(count)
function renderText(count, limit)
{
var rendered_count = options.counterFormat.replace(/%1/, count);

if ( options.renderTotal )
{
rendered_count += '/'+ options.limit;
rendered_count += '/'+ (limit !== undefined ? limit : options.limit);
}

return rendered_count;
Expand All @@ -88,13 +88,14 @@
{
var characterCount = $(element).val().length;
var counter = options.counterSelector ? $(options.counterSelector) : $(element).nextAll("." + options.counterCssClass).first();
var remaining = options.limit - characterCount;
var counterLimit = counter.attr('data-limit');
var remaining = counterLimit - characterCount;
var condition = remaining < 0;

if ( options.increaseCounting )
{
remaining = characterCount;
condition = remaining > options.limit;
condition = remaining > counterLimit;
}

if ( condition )
Expand All @@ -112,7 +113,7 @@
}
}

counter.html(renderText(remaining));
counter.html(renderText(remaining, counterLimit));
}

function bindEvents(element)
Expand Down