diff options
Diffstat (limited to 'microblog.js')
-rw-r--r-- | microblog.js | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/microblog.js b/microblog.js index bcb207a..03e8d11 100644 --- a/microblog.js +++ b/microblog.js @@ -2,17 +2,20 @@ const textarea = document.querySelector('textarea[name="content"]'); const charCount = document.querySelector('#count'); -const maxCount = parseInt(textarea.getAttribute('maxlength')); -if (textarea.value.length > 0) { - const textLength = [...textarea.value].length; - charCount.textContent = maxCount - textLength; -} else { - charCount.textContent = maxCount; -} +if (textarea) { + const maxCount = parseInt(textarea.getAttribute('maxlength')); + + if (textarea.value.length > 0) { + const textLength = [...textarea.value].length; + charCount.textContent = maxCount - textLength; + } else { + charCount.textContent = maxCount; + } -textarea.addEventListener('input', function () { - const textLength = [...this.value].length; + textarea.addEventListener('input', function () { + const textLength = [...this.value].length; - charCount.textContent = maxCount - textLength; -}, false); + charCount.textContent = maxCount - textLength; + }, false); +} |