Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery text area length count?


I have a text area field where i need to provide information about the word count when the user enters some text in the field. Length of the field is supposed to be 500 Characters. Initialy it must show

min characters:100 | 0 of 500 // 0 of 500 must be in red color

and once the user enters come character need to update the count as well. Once the user reaches the count say the min character 100, i need to display

min characters:100 | 100 of 500 // 100 of 500 must be in green color.

How can i do this?? is there any plugin for the same??? let me know your thoughts on this.

like image 432
nimi Avatar asked Dec 22 '10 07:12

nimi


People also ask

How do I count characters in a textarea jQuery?

<textarea id="field" onkeyup="countChar(this)"></textarea> function countChar(val){ var len = val. value. length; if (len >= 500) { val. value = val.

How do I count the number of characters in a string using jQuery?

We can find the length of the string by the jQuery . length property. The length property contains the number of elements in the jQuery object. Thus it can be used to get or find out the number of characters in a string.

How do I limit characters in textarea?

Note − By default, we can enter data in a textarea upto 5,24,288 characters. In some cases, there is a need of putting a limit on the size of characters that can be typed in a textarea. So in that case, we can use maxlength attribute to control the number of characters entered in a textarea.


3 Answers

Simplest way to count:

var count = $("#your_textarea").val().length; 
like image 59
gmoz22 Avatar answered Sep 22 '22 23:09

gmoz22


$("#your-text-area").on('keyup', function(event) {
    var currentString = $("#your-text-area").val()
    $("Your Div").html(currentString.length);
    if (currentString.length <= 500 )  {  /*or whatever your number is*/
       //do some css with your div
    } else {
       //do some different stuff with your div
    }
});

http://api.jquery.com/bind/

like image 39
Cole Avatar answered Sep 24 '22 23:09

Cole


Try to use this plugin feature

jquery-character-counter from jqeasy.com

like image 32
Mohan Ram Avatar answered Sep 25 '22 23:09

Mohan Ram