Skip to content

Instantly share code, notes, and snippets.

@angusgrant
Created October 17, 2022 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angusgrant/ae33791588d237170a5cf303c2aba336 to your computer and use it in GitHub Desktop.
Save angusgrant/ae33791588d237170a5cf303c2aba336 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Character Count</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
body {
margin: 0 auto;
max-width: 40em;
width: 88%;
}
label {
display: block;
width: 100%;
}
textarea {
min-height: 24em;
width: 100%;
}
</style>
</head>
<body>
<h1>Character Count</h1>
<label for="text">Enter your text below.</label>
<textarea id="text"></textarea>
<p>You've written <strong><span id="character-count">0</span> characters</strong>.</p>
<script>
const charCountCounter = document.querySelector('#character-count');
document.addEventListener('input', function (event) {
if (event.target.matches('#text')) {
charCountCounter.textContent = event.target.value.length;
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment