How can I make a "remaining characters" countdown like the one on Twitter with jQuery? And also limit the input to a textarea.
Everything Counts As far as Twitter is concerned, every single character in a Tweet counts as one for the purposes of the character count.
In November 2017 Twitter doubled the available character space from 140 to 280 characters.
Twitter decision to increase its character count from 140 to 280 characters.
What IS Counted: Any character in the text of your post, including spaces. Emojis (1 emoji registers as 2 characters) Hashtags.
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: ','
});
It's a suicide to use jQuery, Mootools or similar for such simple task ... unless you are already using them for something else.
Simple javascript function:
function limitTextCount(limitField_id, limitCount_id, limitNum)
{
var limitField = document.getElementById(limitField_id);
var limitCount = document.getElementById(limitCount_id);
var fieldLEN = limitField.value.length;
if (fieldLEN > limitNum)
{
limitField.value = limitField.value.substring(0, limitNum);
}
else
{
limitCount.innerHTML = (limitNum - fieldLEN) + ' charachter(s) to go.';
}
}
The first parameter is id of the input/textarea. The second - id of the div to display characters left. The third is the limit itself.
And simple HTML:
<input type="text" value="" name="title" id="title" maxlength="100" onkeyup="limitTextCount('title', 'divcount', 100);" onkeydown="limitTextCount('title', 'divcount', 100);">
<br>
<div id="divcount">100 charachter(s) to go..</div>
I added a simple pluralize function, cus nobody likes bad grammar.
function pluralize(count, word){
if (count == 1 || count == -1){
return String(count) + ' ' + word;
}else{
return String(count) + ' ' + word + 's';
}
}
function updateCountdown() {
// 140 is the max message length
var remaining = 140 - jQuery('#micropost_content').val().length;
jQuery('.countdown').text(pluralize(remaining,'character') + ' remaining');
}
...
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.');
}
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);
});
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)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With