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
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`
}