How can I make a \"remaining characters\" countdown like the one on Twitter with jQuery? And also limit the input to a textarea.
Make a span
and textarea
and give them unique selectors (using an ID or class) like so:
<textarea class="message" rows="2" cols="30"></textarea>
<span class="countdown"></span>
And then make an update function like so:
function updateCountdown() {
// 140 is the max message length
var remaining = 140 - jQuery('.message').val().length;
jQuery('.countdown').text(remaining + ' characters remaining.');
}
And make that function run when the page is loaded and whenever the message is changed:
jQuery(document).ready(function($) {
updateCountdown();
$('.message').change(updateCountdown);
$('.message').keyup(updateCountdown);
});
Visit an example and view the source. jQuery makes things like this very simple.
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:
$('#my_textarea').simplyCountable();
Advanced usage:
$('#my_textarea').simplyCountable({
counter: '#counter',
countable: 'characters',
maxCount: 140,
strictMax: false,
countDirection: 'down',
safeClass: 'safe',
overClass: 'over',
thousandSeparator: ','
});
In my implementation, I added the following JS function:
function charactersRemaining(elementID, messageElementID, maxCharacters)
{
var remaining = maxCharacters - $('#' + elementID).val().length;
$('#' + messageElementID).text(remaining + ' characters remaining.');
}
Then I could reuse that function all over the application (for example, if I had the following HTML):
<textarea class="form-control" asp-for="Comments" rows="3"></textarea>
<span id="commentsCharsRemaining" class="text-primary"></span>
Just add these 2 jquery statements to make it work:
$('#Comments').change(function () {
charactersRemaining('Comments', 'commentsCharsRemaining', 2000)
});
$('#Comments').keyup(function () {
charactersRemaining('Comments', 'commentsCharsRemaining', 2000)
});
We ended up doing a custom JS solution that operates on any input with class "limited". It uses an HTML5 custom data attribute ("data-maxlength") to specify the length. Of course, this is not nearly as full-featured as the plugin Ryan mentioned. But we needed multiple, dynamically added counters which didn't seem to be supported by the plugin. Hope that helps.
function addCharacterCount(input) {
var $input = $(input),
maxLength = $input.data('maxlength'),
remaining = maxLength - $input.val().length;
$('<label><input type="text" id="' + input.id + '-counter" size="3" value="' + remaining + '" /> Characters remaining (max ' + maxLength + ')</label>').insertAfter(input);
}
$('form .limited').each(function () {
addCharacterCount(this);
});
$('form .limited').live('keyup', function() {
var $element = $(this),
maxLength = $element.data('maxlength');
$("#" + this.id + "-counter").val(maxLength - $element.val().length);
});
I Updated @Brian McKenna version for multiple textarea/textbox with maxlegth attribute.
Demo here
<textarea class="message" rows="2" cols="30" maxlength="60"></textarea>
<span class="countdown"></span>
<textarea class="message" rows="2" cols="30" maxlength="100"></textarea>
<span class="countdown"></span>
On page load update all count.
jQuery(document).ready(function($) {
updateCountdownAll();
$('.message').live('input', updateCountdown);
});
Make two function for update count.
function updateCountdownAll() {
$('.message').each(function () {
updateCountdown(this);
});
}
function updateCountdown(e) {
var currentElement;
if (e.target) {
currentElement = e.target;
}
else {
currentElement = e;
}
var maxLengh = $(currentElement).attr('maxlength');
var remaining = maxLengh - $(currentElement).val().length;
$(currentElement).nextAll('.countdown:first').text(remaining + ' character(s) remaining.');
}
<form>
<!-- remember to markup your forms properly using labels! -->
<label for="textareaChars">No more than 100 characters</label>
<textarea id="textareaChars" maxlength="100"></textarea>
<p><span id="chars">100</span> characters remaining</p>
</form>
$( document ).ready(function() {
var maxLength = 100;
$('textarea').keyup(function() {
var length = $(this).val().length;
var length = maxLength-length;
$('#chars').text(length);
});
});