Trying to do a quizz page and I need for my “Submit” button to appear once the minimum number of characters have been written in the textarea. Here is what I tried but couldn’t get it to work.
<textarea name="tweet" id="textbox"
rows="13" cols="70" placeholder="" maxlength="250"></textarea><br>
<span id="char_count">0/250</span>
<script>
let textArea = document.getElementById("textbox");
let characterCounter = document.getElementById("char_count");
const minNumOfChars = 0;
const maxNumOfChars = 250;
var text = document.getElementById("buton");
const countCharacters = () => {
let numOfEnteredChars = textArea.value.length;
let counter = minNumOfChars + numOfEnteredChars;
characterCounter.textContent = counter + "/250";
};
textArea.addEventListener("input", countCharacters);
if (counter > 100 ) {
text.style.display = "block";
}
else {
}
</script>
<br><br><br>
<div>
<button class="NextStep" id="buton" style="display:none"
onclick="NextStep()">Finalizare Curs</button>
</div>
<script>
function NextStep() {
location.href =("Cursuri.html")
}
</script>
2
Answers
Your code has all the necessary parts to work but you have restructured it like a Picasso painting!. With some cut-copy-paste in the right place, it works!
First, always make sure to place the script tag at the end of the body tag so that it will wait to first fully load the DOM tree before moving in to script part.
Second, you seem to be wanting to keep track of
counter
variable, and change the button style based on that. But as @mykaf said, you have declared it wrongly inside thecountCharacters
function, so it cannot be accessed outside of that function. Read about scope of variable.Third, you need to check if value of
counter
is greater than 100 each time it is changed, but currently you have put theif-else
part which checks this, outside of thecountCharacters
function. So effectively,counter
value is getting increased but it is not being checked if greater than 100. So you have to also move thatif-else
part inside thecountCharacters
function.Overall, your script tag should looks something like this