JSFiddle - React, Tailwind, and code Playground
HTML
<form action="test.asp">
<textarea id='text'></textarea>
<input type="text" id="varCount" readonly="true" value="" />
<p>Words: <span id="wordCount">0</span></p>
</form>
JavaScript
counter = function() {
var value = $('#text').val();
if (value.length == 0) {
$('#wordCount').html(0);
return;
}
var regex = /[,]/g;
var wordCount = value.trim().replace(regex, ' ').split(' ').length;
$('#wordCount').html(wordCount);
$('#varCount').val(wordCount);
};
$(document).ready(function() {
$('#count').click(counter);
$('#text').change(counter);
$('#text').keydown(counter);
$('#text').keypress(counter);
$('#text').keyup(counter);
$('#text').blur(counter);
$('#text').focus(counter);
});