I am creating the quiz app using JavaScript . I want to change the color of the input button when the user click on that as per the answer. if the answer is true then need to change the color to red and if the answer is wrong then the color need to change to red . Need to select only one option for every question. If the user selected one option it need to highlight green other wise red . if red then it need to highlight the green . Should not give the other chance to user to click on the options everytime to see which option is correct.
Html :
<label class="option"><input type="radio" name="option" value="1" onclick="check()"/> <span id="opt1"></span></label>
<label class="option"><input type="radio" name="option" value="2" onclick="check()" /> <span id="opt2"></span></label>
<label class="option"><input type="radio" name="option" value="3" onclick="check()"/> <span id="opt3"></span></label>
<label class="option"><input type="radio" name="option" value="4" onclick="check()"/> <span id="opt4"></span></label>
<button id="nextButton" class="next-btn" onclick="loadNextQuestion();">Next </button>
**Js onclick function **
var questions = [];
$.ajax({
url: 'http://127.0.0.1:8000/api/?format=json',
type:'GET',
async:true,
dataType: "json",
success: function(data)
{
questions = data ;
loadQuestion();
}
});
function loadQuestion(){
//questionIndex = 0
var questionEl = document.getElementById("question");
var opt1 = document.getElementById("opt1");
var opt2 = document.getElementById("opt2");
var opt3 = document.getElementById("opt3");
var opt4 = document.getElementById("opt4");
questionEl.innerHTML = (currentQuestion + 1) + '. '+ questions[currentQuestion].question;
opt1.innerHTML = questions[currentQuestion].option1;
opt2.innerHTML = questions[currentQuestion].option2;
opt3.innerHTML = questions[currentQuestion].option3;
opt4.innerHTML = questions[currentQuestion].option4;
}
var currentQuestion = 0;
var score = 0;
var totQuestions = 8;
function loadNextQuestion() {
resetColor();
var selectedOption = document.querySelector('input[type=radio]:checked');
if(!selectedOption){
alert('Please select your answer!' );
return;
}
var answer = selectedOption.value;
if(questions[currentQuestion].correct_answer == answer){
score += 10;
}
selectedOption.checked = false;
currentQuestion++;
var nextButton =document.getElementById('nextButton');
if(currentQuestion == totQuestions - 1){
nextButton.innerHTML = 'Finish';
}
var container = document.getElementById('quizContainer');
var resultCont = document.getElementById('result');
if(currentQuestion == totQuestions){
container.style.display = 'none';
resultCont.style.display = '';
resultCont.textContent = 'Your Score: ' + score;
return;
}
loadQuestion(currentQuestion);
}
loadQuestion(currentQuestion);
function check()
{
var selectedOption = document.querySelector('input[type=radio]:checked');
var answer = selectedOption.value;
if(questions[currentQuestion].correct_answer == answer)
{
document.getElementsByName('option').style.color="green";
}
else{
document.getElementsByName('option').style.color="red";
}
}
I just wrote document.getElementsByName('option').style.color="red"; ** but not worked it is showing the error " **Uncaught TypeError: Cannot set property 'color' of undefined
1.What is the wrong with that line. can u please help me guys.