w3resource

jQuery: Limit character input in the textarea including count

jQuery Practical exercise Part - I : Exercise-9

Limit character input in the textarea including count.

Sample HTML Code :

<body>
<form>
<label>Maximum 15 characters</label>
<textarea id="textarea" maxlength="15"></textarea>
<span id="rchars">15</span> characters remaining
</form> </body>

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <meta charset="utf-8">
  <title>Limit character input in the textarea including count</title>
<style type="text/css">
textarea {
  display:block;
  margin:1em 0;
}  
</style>  
</head>
<body>
<form>
<label>Maximum 15 characters</label>
<textarea id="textarea" maxlength="15"></textarea>
  <span id="rchars">15</span> Character(s) Remaining
</form>
</body>
</html>

JavaScript Code :


var maxLength = 15;
$('textarea').keyup(function() {
  var textlen = maxLength - $(this).val().length;
  $('#rchars').text(textlen);
});

See the Pen jquery-practical-exercise-9 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Print a page using jQuery.
Next: Make first word bold of all elements.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/jquery-exercises/part1/jquery-practical-exercise-9.php