You would need to add an event listener which listens for the keyup
(or keydown
) event on the textarea.
Once you have that, you would need to check that the keyCode
matches the return key (13).
After that, its a simple case of counting the length
of the textarea value and splitting its value via the spaces in order to count the words.
var textarea = document.getElementById('textarea');
textarea.addEventListener('keyup', function (e) {
if (e.keyCode === 13) {
var characters = this.value.length;
var words = this.value.split(' ').length;
document.getElementById('characters').value = characters;
document.getElementById('words').value = words;
}
});
<textarea name="TextArea1" cols="20" rows="2" id="textarea"></textarea><br>
<input name="Text1" type="text" id="characters"/>Characters
<input name="Text1" type="text" id="words" />Words