How can I make a "remaining characters" countdown like the one on Twitter with jQuery? And also limit the input to a textarea.
相关问题
- How to fix IE ClearType + jQuery opacity problem i
- jQuery add and remove delay
- Include empty value fields in jQuery .serialize()
- Disable Browser onUnload on certain links?
- how to get selected text from iframe with javascri
Make a
span
andtextarea
and give them unique selectors (using an ID or class) like so:And then make an update function like so:
And make that function run when the page is loaded and whenever the message is changed:
Visit an example and view the source. jQuery makes things like this very simple.
In my implementation, I added the following JS function:
Then I could reuse that function all over the application (for example, if I had the following HTML):
Just add these 2 jquery statements to make it work:
use jquery
save this as jquery.textlimiter.js
usage
I've used Aaron Russell's simply countable jQuery plugin with success; though, if I were to have written it, I would have designed it a bit differently (automatic counter div creation, using a data-maxlength attribute instead of a plugin option, etc).
Simple usage:
Advanced usage:
HTML:
JS:
I added a simple pluralize function, cus nobody likes bad grammar.