DEV Community

Zoya Khan
Zoya Khan

Posted on

HTML Code for Word Counter

<!DOCTYPE html>
<html>
<head>
    <title>Word Counter</title>
    <script>
        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;
        }
    </script>
</head>
<body>
    <h1>Word Counter</h1>
    <p>Enter text to count the number of words:</p>
    <textarea id="textInput" rows="10" cols="50"></textarea>
    <br><br>
    <input type="button" value="Count Words" onclick="countWords()">
    <br><br>
    <p id="wordCountResult"></p>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

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

Latest comments (0)