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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics