When I type or paste words into a textarea, the total number of words will be displayed at the top.
I have managed to count the words when using the element
<textarea id="text"></text>
.
However, I am unable to count the total number of words using
<div id="text"></div>
Is it feasible to calculate all the words within a
<div id="text"></div>
element?
Here is the JavaScript code for counting words:
counter = function() {
var value = $('#text').val();
if (value.length == 0) {
$('#wordCount').html(0);
return;
}
var regex = /\s+/gi;
var wordCount = value.trim().replace(regex, ' ').split(' ').length;
$('#wordCount').html(wordCount);
};
$(document).ready(function() {
$('#text').change(counter);
$('#text').keydown(counter);
$('#text').keypress(counter);
$('#text').keyup(counter);
$('#text').blur(counter);
$('#text').focus(counter);
});
Can anyone assist me in resolving this issue?