My Js code below is a quiz game. Each question takes up a full page, which means we have to change all the content every time the user presses the next button.
My issue is, i can’t seem to get the image to switch to the next by fully replacing it. In my case the new images just go to the bottom of the first.
JavaScript
x
120
120
1
// Questions will be asked//
2
const Questions = [
3
{ id: 0, i: "images/trump.jpg", a: [{ text: "George Washington", isCorrect: false }, { text: "John Adams", isCorrect: false }, { text: "James Madison", isCorrect: false }, { text: "Donald John Trump", isCorrect: true } ] },
4
{ id: 1, i: "http://www.google.com/intl/en_com/images/logo_plain.png", a: [{ text: "Lampang", isCorrect: false, isSelected: false }, { text: "phuket", isCorrect: false }, { text: "Ayutthaya", isCorrect: false }, { text: "Bangkok", isCorrect: true } ] },
5
{ id: 2, i: "http://www.google.com/intl/en_com/images/logo_plain.png", a: [{ text: "surat", isCorrect: false }, { text: "vadodara", isCorrect: false }, { text: "gandhinagar", isCorrect: true }, { text: "rajkot", isCorrect: false } ] }
6
]
7
8
// Set start//
9
var start = true;
10
11
// Iterate//
12
function iterate(id) {
13
14
// Getting the result display section//
15
var result = document.getElementsByClassName("result");
16
result[0].innerText = "";
17
18
//Adding an image//
19
20
var img = document.createElement("img");
21
img.src = Questions[id].i;
22
var src = document.getElementById("image");
23
src.appendChild(img);
24
25
26
// Getting the options//
27
const op1 = document.getElementById('op1');
28
const op2 = document.getElementById('op2');
29
const op3 = document.getElementById('op3');
30
const op4 = document.getElementById('op4');
31
32
33
// Providing option text//
34
op1.innerText = Questions[id].a[0].text;
35
op2.innerText = Questions[id].a[1].text;
36
op3.innerText = Questions[id].a[2].text;
37
op4.innerText = Questions[id].a[3].text;
38
39
// Providing the true or false value to the options//
40
op1.value = Questions[id].a[0].isCorrect;
41
op2.value = Questions[id].a[1].isCorrect;
42
op3.value = Questions[id].a[2].isCorrect;
43
op4.value = Questions[id].a[3].isCorrect;
44
45
var selected = "";
46
47
// Show selection for op1//
48
op1.addEventListener("click", () => {
49
op1.style.backgroundColor = "lightgoldenrodyellow";
50
op2.style.backgroundColor = "lightskyblue";
51
op3.style.backgroundColor = "lightskyblue";
52
op4.style.backgroundColor = "lightskyblue";
53
selected = op1.value;
54
})
55
56
// Show selection for op2//
57
op2.addEventListener("click", () => {
58
op1.style.backgroundColor = "lightskyblue";
59
op2.style.backgroundColor = "lightgoldenrodyellow";
60
op3.style.backgroundColor = "lightskyblue";
61
op4.style.backgroundColor = "lightskyblue";
62
selected = op2.value;
63
})
64
65
// Show selection for op3//
66
op3.addEventListener("click", () => {
67
op1.style.backgroundColor = "lightskyblue";
68
op2.style.backgroundColor = "lightskyblue";
69
op3.style.backgroundColor = "lightgoldenrodyellow";
70
op4.style.backgroundColor = "lightskyblue";
71
selected = op3.value;
72
})
73
74
// Show selection for op4//
75
op4.addEventListener("click", () => {
76
op1.style.backgroundColor = "lightskyblue";
77
op2.style.backgroundColor = "lightskyblue";
78
op3.style.backgroundColor = "lightskyblue";
79
op4.style.backgroundColor = "lightgoldenrodyellow";
80
selected = op4.value;
81
})
82
83
// Grabbing the evaluate button//
84
const evaluate = document.getElementsByClassName("evaluate");
85
86
// Evaluate method//
87
evaluate[0].addEventListener("click", () => {
88
if (selected == "true") {
89
result[0].innerHTML = "True";
90
result[0].style.color = "green";
91
} else {
92
result[0].innerHTML = "False";
93
result[0].style.color = "red";
94
}
95
})
96
}
97
98
if (start) {
99
iterate("0");
100
}
101
102
// Next button and method//
103
const next = document.getElementsByClassName('next')[0];
104
var id = 0;
105
106
107
next.addEventListener("click", () => {
108
109
setTimeout(() => {
110
start = false;
111
if (id < 2) {
112
id++;
113
iterate(id);
114
console.log(id);
115
116
}
117
})
118
119
120
})
JavaScript
1
116
116
1
:root {
2
--primary: #1D1D1F;
3
--secondary: #858786;
4
--erro: #FF5757;
5
text-align: center;
6
align-items: center;
7
align-self: center;
8
font-family: SF Pro Display, SF Pro Icons, AOS Icons, Helvetica Neue, Helvetica, Arial, sans-serif;
9
}
10
11
.column {
12
justify-items: center;
13
justify-content: center;
14
float: left;
15
width: 50%;
16
justify-content: center;
17
}
18
19
.main-container {
20
margin: 50px;
21
border-radius: 20px;
22
background-color: #F5F8FA;
23
}
24
25
26
/* Clear floats after the columns */
27
28
.main-container:after {
29
content: "";
30
display: table;
31
clear: both;
32
}
33
34
.main-container img {
35
width: 320px;
36
height: 320px;
37
border-radius: 20px;
38
object-position: center;
39
object-fit: cover;
40
}
41
42
.center-cropped img {
43
border-radius: 2px;
44
width: 50%;
45
height: 50%;
46
object-position: center;
47
object-fit: cover;
48
}
49
50
.option-container {
51
margin-top: 50%;
52
margin-bottom: 50%;
53
grid-column: 1;
54
margin: 10px;
55
padding: 5px;
56
width: 100%;
57
height: auto;
58
}
59
60
.quiz-image {
61
margin: 10px;
62
padding: 10px;
63
width: 100%;
64
}
65
66
.bottom-left {
67
position: absolute;
68
bottom: 8px;
69
left: 16px;
70
}
71
72
.option {
73
border-radius: 10px;
74
border-width: 0px;
75
margin: 10px;
76
padding: 10px;
77
width: 50%;
78
height: auto;
79
font-size: 1rem;
80
font-weight: 600;
81
color: white;
82
background-color: #1da1f2;
83
}
84
85
.option:hover {
86
background-color: #e1e8ed;
87
}
88
89
.title h1 {
90
font-size: 4rem;
91
font-weight: 400;
92
padding: 10px;
93
color: #1d1d1d;
94
}
95
96
.title h2 {
97
font-size: 1.5rem;
98
font-weight: 400;
99
color: #1D1D1D;
100
}
101
102
h2 {
103
font-size: 3rem;
104
font-weight: 300;
105
color: #1D1D1D;
106
}
107
108
.question-container {
109
margin: 10px;
110
padding: 5px;
111
width: 80vw;
112
height: 10vh;
113
background-color: #c7dddf;
114
font-size: x-large;
115
text-align: center;
116
}
JavaScript
1
22
22
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
<div class="title">
3
<h1>Guess The President</h1>
4
</div>
5
<div class="main-container" >
6
<div class="result"></div>
7
<div class="column">
8
<div class="quiz-image" id="image"></div>
9
</div>
10
<div class="column">
11
<div class="option-container">
12
<button class="option" onclick="" id="op1">option1</button>
13
<button class="option" id="op2">option2</button>
14
<button class="option" id="op3">option3</button>
15
<button class="option" id="op4">option4</button>
16
</div>
17
</div>
18
</div>
19
<div class="navigation">
20
<button class="evaluate">Evaluate</button>
21
<button class="next">Next</button>
22
</div>
Advertisement
Answer
Where you have your
JavaScript
1
2
1
src.appendChild(img);
2
Change it to this:
JavaScript
1
3
1
src.innerHTML = "";
2
src.appendChild(img);
3
This will clear out the previous image and the new one will be added in.
Hope this helps.