var textCharCounter = new Class({ counterObject: null, areaObject: null, areaLimit: 300, areaCounter: 0, initialize: function(areaId, targetId, options) { this.areaLimit = options.limit; this.areaObject = $(areaId); this.counterObject = $(targetId); this.counterObject.innerHTML = this.areaLimit; this.setCountEvents(); this.checkAreaSize(); }, setCountEvents: function(){ this.areaObject.addEvent('keyup',this.checkAreaSize.bind(this)); }, checkAreaSize: function(){ this.areaCounter = this.areaObject.get('value').length; if(this.areaCounter > this.areaLimit){ this.counterObject.innerHTML = 0; this.areaObject.value = this.areaObject.get('value').substring(0,this.areaLimit); }else{ this.counterObject.innerHTML = this.areaLimit - this.areaCounter; } } }); textCharCounter.implement(new Events); textCharCounter.implement(new Options);