Skip to content

Instantly share code, notes, and snippets.

@blackjk3
Created May 16, 2012 19:56
Show Gist options
  • Save blackjk3/2713457 to your computer and use it in GitHub Desktop.
Save blackjk3/2713457 to your computer and use it in GitHub Desktop.
countdown
var countArr = [];
var Countdown = function(options) {
this.max = options.max;
this.obj = options.obj;
this.remaining = this.max - $(this.obj).val().length;
};
Countdown.prototype.updateRemaining = function() {
if (this.remaining > this.max) {
this.remaining = this.max;
}
if ($(this.obj).val().length > this.max) {
this.remaining = 0;
}
if(this.remaining !=0) {
this.remaining = this.max - $(this.obj).val().length;
}
return this.remaining;
};
$(".countdown").each(function() {
$(this).focus(function() {
var c = $(this).attr("class");
var countdownObj = new Countdown({
obj:this,
max:parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0])
});
countArr[$(this).attr("id")] = countdownObj;
}).bind('keyup blur', function() {
var countdownObj = countArr[$(this).attr("id")];
var lettersLeft = countdownObj.updateRemaining();
$("."+ $(this).attr("id")+"_remaining").html("(" + lettersLeft + " characters remaining.)");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment