(function($){
$.fn.textareaCounter = function(options) {
// setting the defaults
// $("textarea").textareaCounter({ limit: 100 });
var defaults = {
limit: 150
};
var options = $.extend(defaults, options);
// and the plugin begins
return this.each(function() {
var obj, text, wordcount, limited;
obj = $(this);
obj.next(".hint").after('<span style="font-size: 11px; font-weight: bold; color:#6a6a6a; clear: both; margin: 3px 0 0 150px; float: left; overflow: hidden;" id="counter-text">Max. '+options.limit+' words</span>');
obj.on("keyup keydown", function() {
text = obj.val();
if(text === "") {
wordcount = 0;
} else {
wordcount = obj.val().split(" ").length;
}
if(wordcount == options.limit) {
obj.next(".hint").next("#counter-text").html('<span style="color: #DD0000;">0 words left</span>');
limited = $.trim(text).split(" ", options.limit);
limited = limited.join(" ");
$(this).val(limited);
} else {
obj.next(".hint").next("#counter-text").html((options.limit - wordcount)+' words left');
}
});
});
};
})(jQuery);
I use above jQuery function to count words that are typing in a textarea. At the moment its working for me. With this script I can count words in all textareas in my forms that limit to 150 words.
But I need to apply this function to textarea to count words with different limit criteria. That mean 1 textarea with 150 words, one with 100 words, one with 60 words etc... can anybody here help me to do this modification?
NOTE: I use this function something like this.. $("textarea").textareaCounter();
countto be... well... a rather nasty swear word