How to count characters remaining with jQuery?

Today, we are going to take a look at a simple jQuery snippet which can count the number of characters the user has entered in the textarea and display the remaining character count, that he/she can input. You may have seen this type feature in twitter while creating a new tweet.

Count Characters Remaining with jQuery

Count Characters Remaining with jQuery

See Also: How to select / deselect all checkboxes using jQuery?


	var totalChars = $('#remaining').text();
	var inputChars = $('#sampleText').val();
	var remainChars = totalChars;
		var inputChars = $(this).val().length;
		remainChars = totalChars - inputChars;


	<div><textarea name="sampleText" id="sampleText" rows="10" cols="50"></textarea></div>
	Characters Remaining: <strong><span id="remaining">140</span></strong>


When the user starts typing inside the textarea, the keyup event gets fired and the script calculates the number of characters remaining by subtracting the user input from the total number of characters and display it.

Click Here to view the demo.


Leave a Comment.