DEV Community

Zoya Khan
Zoya Khan

Posted on

HTML Code for Word Counter

<!DOCTYPE html>
    <title>Word Counter</title>
        function countWords() {
            // Get user input
            var text = document.getElementById("textInput").value;

            // Split text into array of words
            var words = text.split(/\s+/);

            // Count number of words
            var numWords = words.length;

            // Display result to user
            document.getElementById("wordCountResult").innerHTML = "Number of words: " + numWords;
    <h1>Word Counter</h1>
    <p>Enter text to count the number of words:</p>
    <textarea id="textInput" rows="10" cols="50"></textarea>
    <input type="button" value="Count Words" onclick="countWords()">
    <p id="wordCountResult"></p>

Enter fullscreen mode Exit fullscreen mode

This code is developed by Zoya from ACPL Tracking. See the blog here.

Top comments (0)