So I'm applying my recent knowledge of Javascript and the DOM. However, I'm getting an error message saying "addEventListener is not defined" when I attempt to use querySelectorAll or getElementsByClassName for the same button, but for different questions. Here's my code block.
var input = document.getElementsByClassName('userInput');
var submitButton = document.getElementsByClassName('.submit');
var numAnsCorrect = 0;
var numAnsIncorrect = 0;
var questionsRight = document.querySelector('.questionsRight');
var questionsWrong = document.querySelector('.questionsWrong');
submitButton.addEventListener('click', () => {
let submitButton = document.querySelector('.submit');
let userInput = document.querySelector('.userInput');
let response = userInput.value;
response = response.toUpperCase();
var answer = ['SAVANNAH', 'NATHAN DEAL'];
let result = document.querySelector('.question-result');
for (var i = 0; i < answer.length; i++) {
if (response === answer[i]) {
result.textContent += ' That is correct!';
numAnsCorrect++;
questionsRight.textContent = numAnsCorrect;
submitButton.style.display = "none";
} else {
result.textContent += ' That is incorrect';
numAnsIncorrect++;
questionsWrong.textContent = numAnsIncorrect;
submitButton.style.display = "none";
}
}
});
What am I doing wrong? I assumed I would have to loop through the buttons or maybe have another equality operator for button but I'm not too sure.