Created
May 23, 2011 16:56
-
-
Save kascote/987043 to your computer and use it in GitHub Desktop.
TextArea KeyCounter
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
// Contador de caracteres genérico para un textarea | |
// | |
// $('textarea').keycounter(); | |
// usas los valores por defecto.. el nombre del contador es 'counter' y 140 caracteres máximos | |
// | |
// $('textarea').keycounter({'counter': '#area_counter', 'max': 300}); | |
// el contador va a ser el elemento area_counter y tendrá un máximo de 300 caracteres | |
// | |
// Nelson Fernandez - 23.5.2011 - v.0.1 | |
// | |
(function($) { | |
var m = {}; | |
m.o = {}; | |
m.keydown = function(ev) { | |
if ((ev.keyCode == 8) || (ev.keyCode == 9) || ((ev.keyCode >= 35) && (ev.keyCode <= 46))) { return; } | |
if (m.o.t.val().length < m.o.max) { | |
m.o.c.html(m.o.t.val().length); | |
return true; | |
} else { | |
ev.stopPropagation(); | |
return false; | |
} | |
}; | |
m.keyup = function(ev) { | |
m.o.c.html(m.o.t.val().length); | |
}; | |
$.fn.keycounter = function(options) { | |
m.o = $.extend({ 'counter': '#counter', 'max': 140 }, options); | |
m.o.t = $(this); | |
m.o.c = $(m.o.counter); | |
return this.each( function() { | |
$(this).bind('keydown', m.keydown); | |
$(this).bind('keyup', m.keyup); | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment