Skip to content

Instantly share code, notes, and snippets.


codingwithsara/index.html Secret

Created Nov 9, 2018
What would you like to do?
How to Count Characters in Textarea using JavaScript
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Text Counter</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
.container {
width: 500px;
margin-top: 100px;
font-size: 18px;
<div class="container">
<div class="form-group">
<label>Count Up</label>
<textarea class="form-control" rows="5" onkeyup="count_up(this);" maxlength="20"></textarea>
<span class="text-muted pull-right" id="count1">0</span>
<div class="form-group">
<label>Count Down</label>
<textarea class="form-control" rows="5" onkeyup="count_down(this);"></textarea>
<span class="text-muted pull-right" id="count2">30</span>
function count_up(obj) {
document.getElementById('count1').innerHTML = obj.value.length;
function count_down(obj) {
var element = document.getElementById('count2');
element.innerHTML = 30 - obj.value.length;
if (30 - obj.value.length < 0) { = 'red';
} else { = 'grey';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment