Build a Real Time Counter - Build a Real Time Counter

Tell us what’s happening:

I am unable to update char-count using input event listener on text area. Attempted to implement a snippet gathered from MDN example of input event listener ( Element: input event - Web APIs | MDN ) but am stuck on how to implement it correctly to change number in real time.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Real Time Counter</title>
</head>

<body>
    <textarea id = text-input>

    </textarea id = text-input>
    <p id = char-count> Character Count: 0/50
    </p>
</body>


</html>
/* file: styles.css */

/* file: script.js */
/*
P 
R
E
P 
*/
//grabbed the text input box
let textInput = document.getElementById("text-input")
//grabbed the char count text
let charCount = document.getElementById("char-count")

//let countChar = textInput.value.trim().length

//const myText = `Character Count: ${countChar}/50`

//charCount.innerText = myText

textInput.addEventListener("input", updateValue);

function updateValue(e) {
  charCount.innerText = `Character Count: ${e.target.value.trim().length}/50`
}
//let str = document.getElementById('text-input').innerHTML

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:156.0) Gecko/20100101 Firefox/156.0

Challenge Information:

Build a Real Time Counter - Build a Real Time Counter

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-real-time-counter/66bb3e20d3dc5b6d0a21f5dd.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @d3911ready,

An id attribute’s value must be unique, and all attribute values should be enclosed in quotes.

Do attributes belong in an HTML element’s closing tag?

Happy coding

I fixed the issues highlighted in your comment and still facing issues with not having the char count not update properly as I input text in real time

Have you added your script file in your html file?

I did not. this worked thank you. The number updates in code pen but not in FCC when I click run is there a reason for that?

I am not able to pass step 5 even though on code pen my code is changing text color

/*
P 
R
E
P 
*/
//grabbed the text input box
let textInput = document.getElementById("text-input")
//grabbed the char count text
let charCount = document.getElementById("char-count")

textInput.addEventListener("input", updateValue);

function updateValue(e) {
  if(e.target.value.trim().length >= 50)
  {
    textInput.style.color = 'red';
  }
  else if(e.target.value.trim().length < 50){ textInput.style.color = 'black';}
  charCount.innerText = `Character Count: ${e.target.value.trim().length}/50`
}

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --dark-grey: #0a0a23;
  --white: #ffffff;
  --yellow: #f1be32;
}

body {
  background-color: var(--dark-grey);
  color: var(--white);
}

h1 {
  text-align: center;
  margin-top: 20px;
}

.select-container {
  display: block;
  margin: 25px auto;
  padding: 8px;
  border: 4px solid var(--white);
  border-radius: 4px;
  width: 200px;
}

.products-container {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 20px;
}

@media (min-width: 760px) {
  .products-container {
    flex-direction: row;
  }
}

.card {
  background-color: var(--white);
  color: var(--dark-grey);
  border: 4px solid var(--yellow);
  border-radius: 5px;
  padding: 10px;
  width: 200px;
}

I was able to figure it out thank you!