Skip to content

Commit 86d15b3

Browse files
authored
Update script.js
1 parent dfceaa4 commit 86d15b3

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

script.js

+57
Original file line numberDiff line numberDiff line change
@@ -1 +1,58 @@
1+
$(document).ready(function() {
2+
$.ajax({
3+
url: 'fetch_question.php',
4+
method: 'GET',
5+
dataType: 'json',
6+
success: function(data) {
7+
$('#question').text(data.question.question);
18

9+
data.options.forEach(function(option) {
10+
let optionElement;
11+
switch (option.option_type) {
12+
case 'text':
13+
optionElement = `<div class="option">
14+
<input type="radio" name="option" value="${option.option_content}">
15+
<label>${option.option_content}</label>
16+
</div>`;
17+
break;
18+
case 'image':
19+
optionElement = `<div class="option">
20+
<input type="radio" name="option" value="${option.option_content}">
21+
<img src="${option.option_content}" alt="Option Image" style="max-width: 200px;">
22+
</div>`;
23+
break;
24+
case 'video':
25+
optionElement = `<div class="option">
26+
<input type="radio" name="option" value="${option.option_content}">
27+
<video width="320" height="240" controls>
28+
<source src="${option.option_content}" type="video/mp4">
29+
Your browser does not support the video tag.
30+
</video>
31+
</div>`;
32+
break;
33+
case 'link':
34+
optionElement = `<div class="option">
35+
<input type="radio" name="option" value="${option.option_content}">
36+
<a href="${option.option_content}" target="_blank">${option.option_content}</a>
37+
</div>`;
38+
break;
39+
}
40+
$('#options-container').append(optionElement);
41+
});
42+
43+
$('#submit-btn').click(function() {
44+
var selectedOption = $('input[name="option"]:checked').val();
45+
if (selectedOption) {
46+
if (selectedOption === data.question.answer) {
47+
$('#result').text("Correct! The answer is " + data.question.answer + ".");
48+
} else {
49+
$('#result').text("Incorrect. The correct answer is " + data.question.answer + ".");
50+
}
51+
$('#result-container').show();
52+
} else {
53+
alert("Please select an option.");
54+
}
55+
});
56+
}
57+
});
58+
});

0 commit comments

Comments
 (0)