admin管理员组文章数量:1387294
I am trying to create a small quiz program in JavaScript. All of the questions are contained in array and each question is an object containing question itself, choices for user to choose and answer. Quiz loop through the all questions array and print the question and the options for user to choose from. I want choices to be radio buttons. I am struggling to figure out how to populate radio buttons with the questions choices text. Any idea how can do this please?
var userChoices = document.getElementsByTagName('input[type:radio]');
var questions =
[
{
question: "What is the capital of United Kingdom?",
choices: ["Manchester", "Birmingham", "London", "Birmingham"],
answer: 2
},
{
question: "What is the capital of United States?",
choices: ["California", "New York", "Miami", "Florida"],
answer: 1
}
];
for ( var i = 0; i < questions.length; i++ ) {
var question = questions[i].question;
document.write ( question );
var options = questions[i].choices;
for ( var opt in options ) {
for ( var radios in userChoices ) {
userChoices[radios].value = options[opt];
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="radio" name="choices"><br>
<input type="radio" name="choices"><br>
<input type="radio" name="choices"><br>
<input type="radio" name="choices"><br>
</body>
</html>
I am trying to create a small quiz program in JavaScript. All of the questions are contained in array and each question is an object containing question itself, choices for user to choose and answer. Quiz loop through the all questions array and print the question and the options for user to choose from. I want choices to be radio buttons. I am struggling to figure out how to populate radio buttons with the questions choices text. Any idea how can do this please?
var userChoices = document.getElementsByTagName('input[type:radio]');
var questions =
[
{
question: "What is the capital of United Kingdom?",
choices: ["Manchester", "Birmingham", "London", "Birmingham"],
answer: 2
},
{
question: "What is the capital of United States?",
choices: ["California", "New York", "Miami", "Florida"],
answer: 1
}
];
for ( var i = 0; i < questions.length; i++ ) {
var question = questions[i].question;
document.write ( question );
var options = questions[i].choices;
for ( var opt in options ) {
for ( var radios in userChoices ) {
userChoices[radios].value = options[opt];
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="radio" name="choices"><br>
<input type="radio" name="choices"><br>
<input type="radio" name="choices"><br>
<input type="radio" name="choices"><br>
</body>
</html>
Share
Improve this question
asked May 16, 2016 at 10:44
J AkhtarJ Akhtar
6671 gold badge9 silver badges27 bronze badges
12
- Have you heard of Angular.js? I think it will help you – yossico Commented May 16, 2016 at 10:46
-
@yossico: Don't you think
Angular
is too big for this? – Sandeep Nayak Commented May 16, 2016 at 10:46 - Can you provide a JSFiddle example so i could help you ? – Vladimir Drenovski Commented May 16, 2016 at 10:47
- 1 I've just started to learn javascript and been advised to stay away from libraries and frameworks until have a solid understanding of javascript itself. Thanks for your help anyway – J Akhtar Commented May 16, 2016 at 10:51
- 1 @knight: Let me know if my answer is what you are looking at – Sandeep Nayak Commented May 16, 2016 at 11:08
2 Answers
Reset to default 3You can simplify your JS code like below:
var questions =
[
{
question: "What is the capital of United Kingdom?",
choices: ["Manchester", "Birmingham", "London", "Birmingham"],
answer: 2
},
{
question: "What is the capital of United States?",
choices: ["California", "New York", "Miami", "Florida"],
answer: 1
}
];
for ( var i = 0; i < questions.length; i++ ) {
var question = questions[i].question;
document.write ( question );
var options = questions[i].choices;
document.body.appendChild(document.createElement("br"));
var name = "radio"+i;
for ( var opt in options ) {
var radioEle = document.createElement("input");
radioEle.type = "radio";
radioEle.value = options[opt];
radioEle.name = name;
document.body.appendChild(radioEle);
var label = document.createElement("Label");
label.innerHTML = options[opt];
document.body.appendChild(label);
document.body.appendChild(document.createElement("br"));
}
document.body.appendChild(document.createElement("br"));
}
If you need more explanation I will provide it.
<script>
window.onload = function () {
var questions =
[
{
question: "What is the capital of United Kingdom?",
choices: ["Manchester", "Birmingham", "London", "Birmingham"],
answer: 2
},
{
question: "What is the capital of United States?",
choices: ["California", "New York", "Miami", "Florida"],
answer: 1
}
];
var container = document.getElementById('container');
for (var i = 0; i < questions.length; i++) {
var questionContainer = document.createElement('DIV');
questionContainer.textContent = questions[i].question;
var options = questions[i].choices;
for (var opt in options) {
//create radiobutton
//append radiobutton to a div
}
container.appendChild(questionContainer);
}
};
</script>
本文标签: javascriptStoring quiz questions in array of objectsStack Overflow
版权声明:本文标题:javascript - Storing quiz questions in array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744566990a2613108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论