Character and Word Counter Code

Character and Word Counter Code


Coding is difficult? Don't worry, here is a complete working code for the Character and Word Counter. Copy , Paste and enjoy the Calculator.

Copy Below Code From  <!DOCTYPE html>   To   </body>

</html>


 

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Word & Character Counter</title>

    <!-- Bootstrap CSS for responsiveness -->

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">

    <style>

        body {

            font-family: 'Arial', sans-serif;

            background-color: #f4f4f4;

            padding: 20px;

            text-align: center;

        }

        .counter-container {

            max-width: 600px;

            margin: auto;

            background-color: #fff;

            padding: 20px;

            border-radius: 10px;

            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

        }

        textarea {

            width: 100%;

            height: 150px;

            margin: 10px 0;

            padding: 10px;

            box-sizing: border-box;

            border: 1px solid #ccc;

            border-radius: 5px;

        }

        .count-results {

            margin-top: 20px;

            font-size: 18px;

            font-weight: bold;

        }

    </style>

</head>

<body>


    <div class="counter-container">

        <h1>Word & Character Counter</h1>

        <p>Enter your text below to count words and characters:</p>

        <textarea id="inputText" placeholder="Type something..."></textarea>

        

        <div class="count-results">

            Words: <span id="wordCount">0</span> | Characters: <span id="charCount">0</span>

        </div>

    </div>


    <script>

        // Function to count words and characters

        function countWordsAndCharacters() {

            var text = document.getElementById('inputText').value;

            var wordCount = text.split(/\s+/).filter(function(word) {

                return word.length > 0;

            }).length;

            var charCount = text.length;


            document.getElementById('wordCount').textContent = wordCount;

            document.getElementById('charCount').textContent = charCount;

        }


        // Add an event listener for input changes

        document.getElementById('inputText').addEventListener('input', countWordsAndCharacters);

    </script>


</body>

</html>

Post a Comment

0 Comments