Count characters in textarea

textarea character count javascript
textarea remaining character count javascript
how to count characters in textarea in php
angular 7 textarea character count
jquery count characters in input
character count jsfiddle
textbox character counter jquery
textarea character limit javascript

I want to count characters in a textarea, so I just made:

<textarea id="field" onkeyup="countChar(this)"></textarea>

function countChar(val){
     var len = val.value.length;
     if (len >= 500) {
              val.value = val.value.substring(0, 500);
     } else {
              $('#charNum').text(500 - len);

What's wrong with my piece of code? It does not work! Well, that was a newbie handwriting, need a help.

What errors are you seeing in the browser? I can understand why your code doesn't work if what you posted was incomplete, but without knowing that I can't know for sure.

<!DOCTYPE html>
    <script src=""></script>
      function countChar(val) {
        var len = val.value.length;
        if (len >= 500) {
          val.value = val.value.substring(0, 500);
        } else {
          $('#charNum').text(500 - len);

    <textarea id="field" onkeyup="countChar(this)"></textarea>
    <div id="charNum"></div>


... works fine for me.

Edit: You should probably clear the charNum div, or write something, if they are over the limit.

Displaying The Character Count of a Textarea, Forked from [Jason Saba]('s Pen [Displaying The Character Count of a Textarea]( Count Textarea Characters with JavaScript I've been playing around with the Twitter API lately and, as you know, Tweets can only be 140 characters long. I wanted to use a textarea element, not an input type="text", so I couldn't just use maxchars to limit the number of characters.

Improved version based on Caterham's function:

$('#field').keyup(function () {
  var max = 500;
  var len = $(this).val().length;
  if (len >= max) {
    $('#charNum').text(' you have reached the limit');
  } else {
    var char = max - len;
    $('#charNum').text(char + ' characters left');

Character Counting Remaining on Textarea - JSFiddle, <textarea id="textarea" rows="8" cols="30" maxlength="99" ></textarea>. 2. <div id="textarea_feedback"></div>. 3. ​. JavaScript + jQuery 1.7.1 Tidy. 11. 1. If you wish to restrict the user not to type more than 250 characters in text area/textbox change the count value as 250. This will set the length or size of text area or text box. Example:var count = "250";

⚠️ The accepted solution is flawed.

Here are two scenarios where the keyup event will not get fired:

  1. The user drags text into the textarea.
  2. The user copy-paste text in the textarea with a right click (contextual menu).

Use the HTML5 input event instead for a more robust solution:

<textarea maxlength='140'></textarea>

JavaScript (demo):

const textarea = document.querySelector("textarea");

textarea.addEventListener("input", event => {
    const target = event.currentTarget;
    const maxLength = target.getAttribute("maxlength");
    const currentLength = target.value.length;

    if (currentLength >= maxLength) {
        return console.log("You have reached the maximum number of characters.");

    console.log(`${maxLength - currentLength} chars left`);

And if you absolutely want to use jQuery:

$('textarea').on("input", function(){
    var maxlength = $(this).attr("maxlength");
    var currentLength = $(this).val().length;

    if( currentLength >= maxlength ){
        console.log("You have reached the maximum number of characters.");
        console.log(maxlength - currentLength + " chars left");

Really simple jQuery character countdown in textarea, Looking to build a Twitter-like textarea character countdown to show your Mix characters with line breaks and the text counter counts wrongly. CharacterCountControl (optional) – By default the plugin will display character count below the TextArea, but user has option to explicitly specify the Character Count Control. Note : The character count control can only HTML SPAN or DIV. 3. DisplayCharacterCount (optional) – Default true.

HTML sample, used wherever I need a counter, notice the relevance of IDs of textarea and second span : id="post" <-> id="rem_post" and the title of the span that holds the desired characters amount of each particular textarea

<textarea class="countit" name="post" id="post"></textarea>
  <span>characters remaining: <span id="rem_post" title="1000"></span></span>

JavaScript function, usually placed before </body> in my template file, requires jQuery

$(".countit").keyup(function () {
  var cmax = $("#rem_" + $(this).attr("id")).attr("title");

  if ($(this).val().length >= cmax) {
    $(this).val($(this).val().substr(0, cmax));

  $("#rem_" + $(this).attr("id")).text(cmax - $(this).val().length);


Javascript: Calculate Characters Remaining In A TextArea, By rendering the HTML page in the browser, you can observe that we have a fully functional, albeit hopelessly ugly, TextArea counting  Online Character Count Tool. A while back I needed to count the amount of letters that a piece of text in an email template had (to avoid passing any character limits). Unfortunately, I could not think of a quick way to do so on my macbook and I therefore turned to the Internet.

this worked fine for me.

$('#customText').on('keyup', function(event) {
   var len = $(this).val().length;
   if (len >= 40) {
      $(this).val($(this).val().substring(0, len-1));

jQuery: Limit character input in the textarea including count , Limit character input in the textarea including count. Sample HTML Code : <body​> <form> <label>Maximum 15 characters</label> <textarea id="  A text area with a maximum length of 50 characters: <textarea maxlength="50">. Enter text here </textarea>. Try it Yourself ».

How to Count Characters in Textarea using JavaScript, The maxlength attribute specifies the maximum length (in characters) of a text area. Browser Support. The numbers in the table specify the first browser version that  This is my code: function textCounter(field, countfield, maxlimit) { if (field.value.length > maxlimit) { field.value = field.value.substring(0, 160); field.blur(); field.focus(); return false; } else { countfield.value = maxlimit - field.value.length; } } How can I display how many characters are remaining from a certain text box with a limit of 160?

HTML textarea maxlength Attribute, Select the TOOLS menu and then WORD COUNT. A dialogue box will appear containing the character count. Character counting tools many times will provide additional information, such as the character count with and without spaces. In addition, many of the programs will perform a character count of a selected text. To do this, select the text requiring a character count and simply open the character count tool.

Live Character Counter using JavaScript, I have one requirement, Need to count the characters in the textarea field when typing. strlen( ), this string operation only count the total characters. But My requirement need the count the characters when typing. Kindly Help me with solution and Thanks in advance.

  • In the future, please edit your question or use the comment feature under answers to add additional information or clarification. Answers should be posts that directly solve your problem. If you post a solution to your own problem, accept your solution as the correct answer. I've removed a lot of your 'answers', as you've accepted another.
  • JQuery Character Limit Counter
  • really? I have just a span instead of div for id='charNum', let me see again!
  • After seeing your complete code, what were you expecting it to do differently? What is "broken" about it?
  • you can remove the "else", just cause we hit 500 doesn't mean that we should prevent #charNum to display the value, the "else" will make the number stop at 1 instead of 0 as it is right now.
  • @Joakim, I removed the else but, in this case, I'll be getting negative nums!
  • Maybe just add something to the if, setting charNum to say "You've reached the limit", or something like that.
  • Don't use this. This solution is flawed as pointed out by Etienne Martin.
  • Didn't know of the input event. Thanks
  • A quick question (haven't tried your code/method yet) will this work for voice input?
  • @Syfer Absolutely.
  • I will try this out. I needed something like this for a small project at work.