i was wondering how i could get this to be live? I have a text box that is fetched via ajax, and its excluding already loaded scripts i have cause of the dom I’m assuming. Any who, I’ve played with .live() but i usually have an element to hook it to ex/ $('blah').live();, but i found myself scratching my head on this one lol
(function($) {
$.fn.charCount = function(options){
// default configuration properties
var defaults = {
allowed: 140,
warning: 25,
css: 'counter',
counterElement: 'span',
cssWarning: 'warning',
cssExceeded: 'exceeded',
counterText: ''
};
var options = $.extend(defaults, options);
function calculate(obj){
var count = $(obj).val().length;
var available = options.allowed - count;
if(available <= options.warning && available >= 0){
$(obj).next().addClass(options.cssWarning);
} else {
$(obj).next().removeClass(options.cssWarning);
}
if(available < 0){
$(obj).next().addClass(options.cssExceeded);
} else {
$(obj).next().removeClass(options.cssExceeded);
}
$(obj).next().html(options.counterText + available);
};
this.each(function() {
$(this).after('<'+ options.counterElement +' class="' + options.css + '">'+ options.counterText +'</'+ options.counterElement +'>');
calculate(this);
$(this).keyup(function(){calculate(this)});
$(this).change(function(){calculate(this)});
});
};
})(jQuery);
jQuery “live” (deprecated) or “on” works just with click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup events.
If you just need to use just
$('selector').live()use this library:LiveQuery: https://github.com/brandonaaron/livequery