비전공자 개발일기

Live Word Counter 본문

Javascript

Live Word Counter

HiroDaegu 2022. 11. 30. 13:07
728x90
SMALL

<html lang="en">

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>LIVE WORD COUNTER</title>
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&display=swap" rel="stylesheet" />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <!-- Script -->
    <script defer src="main.js"></script>
</head>

<body>
    <div class="wrapper">
        <div class="container">
            <textarea id="input-textarea" rows="12" placeholder="Start Typing here..."></textarea>
        </div>
        <div class="count">
            <div>
                <h5 id="word-count">0</h5>
                <p>Words</p>
            </div>
            <div>
                <h5 id="charac-count">0</h5>
                <p>Characters</p>
            </div>
        </div>
    </div>
</body>

</html>
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Poppins", sans-serif;
  }
  body {
    background-color: #8b5ceb;
  }
  .wrapper {
    position: absolute;
    width: 90vmin;
    transform: translate(-50%, -50%);
    top: 50%;
    left: 50%;
  }
  .container {
    background-color: #ffffff;
    padding: 50px 30px 80px 30px;
    border-radius: 8px;
    box-shadow: 0 30px 50px rgba(30, 21, 49, 0.3);
  }
  textarea {
    width: 100%;
    border: none;
    resize: none;
    outline: none;
    font-size: 16px;
    line-height: 28px;
    color: #0e101a;
  }
  .count {
    background-color: #000000;
    width: 80%;
    padding: 20px;
    position: relative;
    transform: translate(-50%, -50%);
    left: 50%;
    display: flex;
    justify-content: space-around;
    text-align: center;
    border-radius: 5px;
    box-shadow: 0 20px 40px rgba(30, 21, 49, 0.4);
  }
  .count p {
    color: #a0a0c0;
  }
  .count h5 {
    color: #ffffff;
    font-size: 32px;
  }
let inputTextArea = document.getElementById("input-textarea");
let characCount = document.getElementById("charac-count");
let wordCount = document.getElementById("word-count");

inputTextArea.addEventListener("input", () => {
  characCount.textContent = inputTextArea.value.length;
  let txt = inputTextArea.value.trim();
  wordCount.textContent = txt.split(/\s+/).filter((item) => item).length;
});
728x90
LIST

'Javascript' 카테고리의 다른 글

New Year 2023 Countdown  (0) 2022.12.02
Dictionary App  (0) 2022.12.01
Search Highlight Text  (0) 2022.11.29
Blob Maker  (0) 2022.11.25
Flip a Coin  (0) 2022.11.24