I creating a quiz website with 2 questions for now. User will be provided a question and radio buttons to select the answer. Whatever the user selects will be stored in a cookie which will later be provided to php for database retrieval.status array is used to check whether first option was checked or second. When I select the previous button the previous question gets diplayed but the radio button does not change that is it will be same as the second question.
<html>
<head>
<script>
var count = 1;
var status = new Array();
function calculateInput() {
if (document.getElementById('radio1').checked) {
status[count] = 1;
ans1_value = document.getElementById('radio1').value;
document.cookie = count + "=" + ans1_value + ";"
} else
if (document.getElementById('radio2').checked) {
status[count] = 2;
ans1_value = document.getElementById('radio2').value;
document.cookie = count + "=" + ans1_value + ";"
}
}
function myFunction() {
count = count + 1;
if (count == 2) {
x = document.getElementById("questions"); // Find the element
x.innerHTML = "Your PR firm has been hired to promote the reopening of a historic luxury hotel in your town. In addition to producing the opening night gala event, your team will be creating a theme for the evening. Would you rather:"; // Change the content
document.getElementById('Answer0').firstChild.nodeValue = 'Discover more about the hotel’s history to develop the theme. (Attention to detail)';
document.getElementById('Answer1').firstChild.nodeValue = 'Research how the renovation team preserved the building’s original details for inclusion in the event’s press kit. (Research)';
document.getElementById("radio1").checked = true;
document.getElementById("radio1").value = "321";
document.getElementById("radio2").value = "311";
} else if (count > 2) {
window.location.href = "http://localhost/quiz/same_page_implementation/final.php";
}
}
function calculatePrev() {
count = count - 1;
if (count == 1) {
x = document.getElementById("questions"); // Find the element
x.innerHTML = "You are a project manager in an architecture firm that’s designing the world’s largest Ferris Wheel—one that will tower over the São Paulo skyline. Which of the following tasks would you most want to handle?"; // Change the content
document.getElementById('Answer0').firstChild.nodeValue = 'Meeting with the project’s leaders weekly to ensure safety requirements and budget targets are being met. (Planning)';
document.getElementById('Answer1').firstChild.nodeValue = 'Meeting each week with the project’s world-famous architect to brief him on the team’s progress. (Administration)';
if (status[count] == 1) {
document.getElementById("radio1").checked = true;
} else if (status[count] == 2) {
document.getElementById("radio2").checked = true;
}
document.getElementById("radio1").value = "325";
document.getElementById("radio2").value = "310";
}
}
</script>
</head>
<body>
<p id="questions">
You are a project manager in an architecture firm that’s designing the world’s largest Ferris Wheel—one that will tower over the São Paulo skyline. Which of the following tasks would you most want to handle?
</p>
<input type="radio" name="question1" value="325" id="radio1">
<label for="radio1" id="Answer0">Meeting with the project’s leaders weekly to ensure safety requirements and budget targets are being met. (Planning)</label>
<br>
<input type="radio" name="question1" value="310" id="radio2">
<label for="radio2" id="Answer1">Meeting each week with the project’s world-famous architect to brief him on the team’s progress. (Administration)</label>
<br>
<button type="button" onclick=" calculateInput(); myFunction()">Next</button>
<button type="button" onclick=" calculatePrev()">Previous</button>
</body>
</html>