Created
January 16, 2012 19:43
-
-
Save tmilewski/1622584 to your computer and use it in GitHub Desktop.
Manages character counts for a textarea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage | |
$("textarea").counter | |
elements: | |
counter: '.note-area p.counter' | |
# Manages Character Counts | |
jQuery.fn.counter = (opts) -> | |
defaults = | |
elements: | |
counter: '.counter' | |
css: | |
warning: 'warning' | |
exceeded: 'exceeded' | |
limits: | |
allowed: 300 | |
warning: 75 | |
copy: | |
prepend: '' | |
append: ' characters remaining' | |
opts = $.extend defaults, opts | |
opts.elements.textarea = $ this | |
opts.elements.counter = $ opts.elements.counter | |
update = -> | |
used = opts.elements.textarea.val().length | |
remain = opts.limits.allowed - used | |
if remain <= opts.limits.warning and remain >= 0 | |
opts.elements.counter.addClass opts.css.warning | |
else | |
opts.elements.counter.removeClass opts.css.warning | |
if remain < 0 | |
opts.elements.counter.addClass opts.css.exceeded | |
else | |
opts.elements.counter.removeClass opts.css.exceeded | |
opts.elements.counter.html "#{opts.copy.prepend}#{remain}#{opts.copy.append}" | |
this.keyup -> update() | |
this.change -> update() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment