/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Limit Number of Characters in a TextArea</title> <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script> <script type='text/javascript'> $(document).ready(function() { $('#note').keyup(function() { var max=140; var len = this.value.length; if (len >= max) { this.value = this.value.substring(0, max); } $('#charLeft').text(max - len); }); }); </script> </head> <body> <textarea id="note" cols="20" rows="8"></textarea><br/> (Maximum characters: 140)<br/> <span id="charLeft"> </span> Characters left </body> </html>