Created
May 16, 2012 19:56
-
-
Save blackjk3/2713457 to your computer and use it in GitHub Desktop.
countdown
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
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