const textarea = document.getElementById('textarea');
const show_counter = document.getElementById('show_counter');
const max_length = textarea.getAttribute('maxlength');
textarea.addEventListener('input', function (e) {
const counts = this.value.length;
show_counter.innerHTML = `${counts}/${max_length}`;
});
We have created a textarea and div tag in our HTML and using their ids we are getting them our javascript code. Full HTML code to show the counts of characters in a textarea is below.
<textarea id="textarea" maxlength="20" placeholder="Enter some text..."></textarea>
<div id="show_counter"></div>
<script>
const textarea = document.getElementById('textarea');
const show_counter = document.getElementById('show_counter');
const max_length = textarea.getAttribute('maxlength');
textarea.addEventListener('input', function (e) {
const counts = this.value.length;
show_counter.innerHTML = `${counts}/${max_length}`;
});
</script>
0 Comments