Posted By

edwardhotchkiss on 01/31/11


Tagged

javascript character jquery counter twitter


Versions (?)

Who likes this?

17 people have marked this snippet as a favorite

malachaifrazier
Internet-Uzver
desimon
Some0ne
neex123
virendrakryadav
czehner
Balamir
masta
miguelcanijo
clac
ringo380
catrinho
unibox
Priestd09
maidul
o0110o


Twitter like Character Counter


 / Published in: jQuery
 

  1. (function($) {
  2.  
  3. $.fn.CharacterCounter = function(options) {
  4.  
  5. var defaults = {
  6.  
  7. allowed : 140,
  8. counterText : "Characters left: ",
  9. overlimitClass : "overlimit_txt",
  10. pID : "#character_count",
  11. txtColor : "#ff0000"
  12.  
  13. };
  14.  
  15. var options = $.extend(defaults, options);
  16.  
  17. function calculate(obj) {
  18. var count = $(obj).val().length;
  19. var available = options.allowed - count;
  20. if (available == 0) {
  21. $(options.pID).addClass(options.overlimitClass);
  22. } else if (available > 0) {
  23. $(options.pID).removeClass(options.overlimitClass);
  24. }
  25. $(options.pID).html(options.counterText + available);
  26. }
  27.  
  28. calculate(this);
  29.  
  30. $(this).keyup(function() { calculate(this) });
  31. $(this).change(function(){ calculate(this) });
  32.  
  33. };
  34.  
  35. })(jQuery);

Report this snippet  

You need to login to post a comment.