I am trying to make a trivia game using an api, and my elements are not clickable. After I make the api call, I am looping through my responses and using jQuery I am making each response into an html p tag element. My next step is to determine which response is the correct one, but first I want to test each element with a click listener. This is where I am currently stuck. There is no response when I click any p tag. There should be a message logged into the console. Any idea's to help me out?
$(document).ready(onReady);
function onReady() {
console.log("jQuery in tha house!");
$("#btn").on("click", askQuestions);
$("#answer").on("click", determineCorrect);
}
let axiosbtn = document.querySelector("#axios");
let url = "https://opentdb.com/api.php?amount=1";
function shuffle(array) {
let currentIndex = array.length,
temporaryValue,
randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
let askQuestions = () => {
axios
.get(url)
.then(res => {
$("#question").html(res.data.results[0].question);
rightAnswer = res.data.results[0].correct_answer;
let answers = [rightAnswer];
let wrongAnswers = res.data.results[0].incorrect_answers;
wrongAnswers.forEach(wrong => {
answers.push(wrong);
});
let newAnswers = [];
$.each(answers, (i, answer) => {
newAnswers.push("<p id='answer'>" + answer + " " + "</p>");
});
shuffle(newAnswers);
$("#answers").html(newAnswers.join(""));
console.log(shuffle(newAnswers));
console.log(rightAnswer);
})
.catch(err => {
console.log("Error", err);
});
};
let determineCorrect = () => {
console.log("Determine Correct");
};
<body>
<header>
<h1 id="title">Random Trivia</h1>
</header>
<div>
<button id="btn">Get Random Trivia Question</button>
<h2 id="question"></h2>
</div>
<div id="answers"></div>
</body>
</html>