JSFiddle - React, Tailwind, and code Playground

by mlms13

HTML

<textarea id="test" rows="12" cols="48"></textarea>
<p id="result"></p>

JavaScript

var inpt = document.getElementById('test'),
    p = document.getElementById('result');

function getWordCount(text) {
    var count = 0, wordStarted = false, i;
    
    for (i = 0; i < text.length; i++) {
        if (text.charAt(i) !== ' ' && text.charAt(i) !== '\n') {
            if (!wordStarted) {
                count++;
                wordStarted = true;
            }
        } else {
            wordStarted = false;
        }
    }
    return count;
}
function updateWordCount() {
    var result = document.createTextNode(getWordCount(inpt.value));
    
    if (p.firstChild) {
        p.removeChild(p.firstChild);
    }
    
    p.appendChild(result);
}

inpt.onkeyup = updateWordCount;
inpt.onblur = updateWordCount;