Skip to content

Instantly share code, notes, and snippets.

@fieldoffice
Last active October 19, 2021 13:01
Show Gist options
  • Save fieldoffice/abe7840124bcd45992f16f41fbf3edad to your computer and use it in GitHub Desktop.
Save fieldoffice/abe7840124bcd45992f16f41fbf3edad to your computer and use it in GitHub Desktop.
Text Area Character Count
<textarea id="js-message" rows="4" maxlength="300"></textarea>
<div id="js-counter" class="message__counter">0 / 300</div>
<script>
// Character count
var message = document.getElementById('js-message');
var counter = document.getElementById('js-counter');
message.addEventListener('input', function (e) {
var target = e.target;
var maxLength = target.getAttribute('maxlength');
var currentLength = target.value.length;
counter.innerText = currentLength + ' / ' + maxLength;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment