JQUERY | HOW TO COUNT NUMBER OF CHARACTERS IN TEXTAREA (YOUTUBE, TWITTER)
Step 1 (count number of characters in textarea)
Our first step is to add the following code in our html page
< label for = "description" >Description:</ label > < textarea name = "description" id = "description" cols = "60" rows = "5" maxlength = "255" placeholder = "Describe aboutsomething..." ></ textarea > < div id = "characterLeft" ></ div >
|
Here we add
maxlength="255"
attribute in the textarea tag, which means thistextarea takes text that contain 255 characters. More than 255 character will be ignored. After that we add a div element withid="characterLeft"
which will show the number to exceed the limit of the textarea that we define in maxlength attribute.Step 2
Our second step is to add the following jQuery code in our script file.
$( '#characterLeft' ).text( '255 characters left' ); $( '#description' ).keyup( function () { var max = 255; var len = $( this ).val().length; if (len >= max) { $( '#characterLeft' ).text( ' you have reached the limit' ); } else { var ch = max - len; $( '#characterLeft' ).text(ch + ' characters left' ); } |
0 comments