Open In App

How to create Word, Letter and Digit Counter Card using JavaScript and Tailwind CSS ?

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Word Digit and Letter Counter Application allows users to input text and dynamically count the number of words, letters, and digits in the text. Additionally, it provides the “Clear” button to reset the text and clear all counts. The HTML code structures the document, while Tailwind CSS classes handle styling, and JavaScript adds interactivity to the Word, Letter, and Digit Counter applications.

Output Preview: Let us have a look at how the final output will look like.

aaaaaaaaaaaaa

Approach

  • Integrate the Tailwind CSS via CDN Link in your HTML code. Use different HTML elements to structure the web page and style them using different Tailwind utilities.
  • In the <body> tag, Set the background color to gray (bg-gray-100), making the body occupy the full height of the screen (h-screen), and centering its content both horizontally and vertically (flex justify-center items-center).
  • The <textarea> tag creates an input field for user text input, styled with Tailwind CSS classes for width, height, padding, margin-bottom, border, rounded corners, and focus styles (w-full h-40 px-4 py-2 mb-4 border border-gray-300 rounded-md resize-none focus:outline-none focus:border-blue-500). It also includes a placeholder text for user guidance.
  • The clearText() function clears the text in the text area when the “Clear” button is clicked. Initial counts are displayed upon page load.
  • The updateCounts() function is called whenever there’s an input event in the text area. This function calculates and updates the word, letter, and digit counts.
  • The textarea allows users to input text which is then processed to count words, letters, and digits.

Example: Implementation of Designing a Word, Digit, and Letter Counter in Tailwind CSS

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Word, Letter and Digit Counter</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
 
<body class="bg-gray-100 h-screen flex
             justify-center items-center">
    <div class="max-w-lg w-full bg-white p-8 rounded-lg
                shadow-lg border-2 border-green-500">
        <h1 class="text-2xl font-semibold mb-4">
            Word, Letter, and Digit Counter
        </h1>
        <textarea id="text-input"
                  class="w-full h-40 px-4 py-2 mb-4 border
                         border-gray-300 rounded-md resize-none
                         focus:outline-none focus:border-blue-500"
                  placeholder="Enter text here...">
          </textarea>
        <div class="flex justify-between">
            <div>
                <p class="text-sm text-gray-500 mb-1">
                      Word Count
                  </p>
                <p id="word-count" class="text-lg font-semibold">0</p>
            </div>
            <div>
                <p class="text-sm text-gray-500 mb-1">Letter Count</p>
                <p id="letter-count" class="text-lg font-semibold">0</p>
            </div>
            <div>
                <p class="text-sm text-gray-500 mb-1">Digit Count</p>
                <p id="digit-count" class="text-lg font-semibold">0</p>
            </div>
        </div>
        <button id="clear-button"
                class="mt-4 px-4 py-2 bg-red-500
                       text-white rounded-md
                       hover:bg-red-600
                       focus:outline-none">
              Clear
          </button>
        <button id="copy-button"
                class="mt-4 px-4 py-2 bg-blue-500 text-white
                       rounded-md hover:bg-blue-600
                       focus:outline-none">
              Copy Text
        </button>
        <a id="download-link"
           class="mt-4 px-4 py-2 bg-green-500 text-white rounded-md
                  hover:bg-green-600 focus:outline-none"
           download="text.txt">
              Download as File
          </a>
    </div>
    <script>
        function updateCounts() {
            const text = document.getElementById('text-input')
                                 .value;
            const wordCount = text.trim() === '' ? 0 : text.trim()
                                  .split(/\s+/).length;
            const letterCount = text.replace(/[^a-zA-Z]/g, '').length;
            const digitCount = text.replace(/\D/g, '').length;
            document.getElementById('word-count')
                    .textContent = wordCount;
            document.getElementById('letter-count')
                    .textContent = letterCount;
            document.getElementById('digit-count')
                    .textContent = digitCount;
        }
        function clearText() {
            document.getElementById('text-input').value = '';
            updateCounts();
        }
        function copyText() {
            const textArea = document.getElementById('text-input');
            textArea.select();
            document.execCommand('copy');
        }
        function downloadText() {
            const text = document.getElementById('text-input').value;
            const blob = new Blob(, { type: 'text/plain' });
            const url = URL.createObjectURL(blob);
            const downloadLink = document.getElementById('download-link');
            downloadLink.href = url;
        }
        document.getElementById('text-input')
                .addEventListener('input', updateCounts);
        document.getElementById('clear-button')
                .addEventListener('click', clearText);
        document.getElementById('copy-button')
                .addEventListener('click', copyText);
        document.getElementById('download-link')
                .addEventListener('click', downloadText);
        updateCounts();
    </script>
</body>
 
</html>


Output:

wldgif

Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads