In this article, I will show you to calculate how many words have I typed in a textbox using javaScript. With the help of below code, when the user enters some text in a textbox it calculate word count on a JavaScript keyup event.
Example:
<script type="text/javascript">
function countwords() {
var val = document.getElementById("txtinput").value;
var words = 0, chars = 0;
if (val != "") {
words = val.replace(/\s+/gi, ' ').split(' ').length; // Count words
chars = val.length; // Count characters
}
document.getElementById("divAlert").innerHTML = words;
}
</script>
<h2>counting words in a essay</h2>
<br />
<input type="text" id="txtinput" onkeyup="countwords()" style="width: 250px; height: 30px;" /><br />
<div id="divAlert" style="text-align: left; margin-bottom: 10px; color: red">
</div>
Output:
Post your comments / questions
Recent Article
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
Related Article