I am trying to build html using ajax and after the html is built am trying to add/remove class based on a condition but the class is not getting added/removed. Can anyone please help me on this. Below is my code. showslide function is where I am trying to add/remove class
HTML -
<body class="quiz">
<h1>Quiz on Important Facts</h1>
<div class="quiz-container">
<div id="quiz"></div>
</div>
<button id="previous">Previous Question</button>
<button id="next">Next Question</button>
<button id="submit">Submit Quiz</button>
<div id="results"></div>
</body>
Script -
$(document).ready(function(){
var quizContainer = $('#quiz');
var resultsContainer = $('#results');
var submitButton = $('#submit');
var output = [];
var mq ='';
function buildQuiz(){
$.ajax({
url: './json/qa.json',
success: function(data){
//console.log(data.myQuestions);
mq = data.myQuestions;
mq.forEach(
(currentQuestion, questionNumber) => {
const answers = [];
for(letter in currentQuestion.answers){
answers.push(
`<label>
<input type="radio" name="question${questionNumber}" value="${letter}">
${letter} :
${currentQuestion.answers[letter]}
</label>`
);
}
output.push(
`<div class="slide">
<div class="question"> ${currentQuestion.question} </div>
<div class="answers"> ${answers.join("")} </div>
</div>`
);
quizContainer.append(output.join(''));
// console.log("output"+output.join(''))
});
}
})
}
buildQuiz();
const slides = $('#quiz').find(".slide");
var currentSlide = 0;
function showSlide(n) {
console.log("slides - "+slides);
//Here am tring to add/remove class
$('#quiz').find('.slide').eq(0).removeClass('active-slide');
$('#quiz').find('.slide').eq(n).addClass('active-slide');
}
showSlide(currentSlide);
})
When I do a console log of 'slides' it is returning [object object]
JSON -
{
"myQuestions" : [
{
"question": "Who invented JavaScript?",
"answers": {
"a": "Douglas Crockford",
"b": "Sheryl Sandberg",
"c": "Brendan Eich"
},
"correctAnswer": "c"
},
{
"question": "Which one of these is a JavaScript package manager?",
"answers": {
"a": "Node.js",
"b": "TypeScript",
"c": "npm"
},
"correctAnswer": "c"
},
{
"question": "Which tool can you use to ensure code quality?",
"answers": {
"a": "Angular",
"b": "jQuery",
"c": "RequireJS",
"d": "ESLint"
},
"correctAnswer": "d"
}
]
}