Last active
April 11, 2022 16:03
-
-
Save tyte/62216eef2f7a0b01f08c8c49fd8148fa to your computer and use it in GitHub Desktop.
JS Character count in textarea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> | |
// Your code goes here... | |
let textArea = document.getElementById('text'); | |
let counterText = document.getElementById('character-count'); | |
function showCount() { | |
counterText.textContent = textArea.value.length; | |
} | |
textArea.oninput = showCount; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment