I’m creating a questionnaire, this is my html page for it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Questionnaire</title>
<link rel="stylesheet" href="questionnaire.css">
</head>
<body>
<div class="row">
<div class="side"></div>
<div id="middle"></div>
<div class="side"></div>
</div>
<script src="index.js"></script>
</body>
</html>
And this is the javascript code in the index.js file:
const contents = document.getElementById('middle');
function loadPage(){
console.log("Hello")
getData();
}
async function getData(){
const response = await fetch("example-questionnaire.json");
const data = await response.json();
console.log(data)
makePage(data);
}
getData();
//Create page
function makePage(getObject){
for(let i = 0; i < getObject.length; i++){
let card = document.createElement('div');
card.id = getObject[i].id;
card.classList.add('card');
contents.appendChild(card);
//input object and text object
const questionText = document.createElement('label');
questionText.setAttribute('for', getObject[i].id)
questionText.classList.add('label');
questionText.textContent = getObject[i].title;
let questionType = getObject[i].input;
switch (questionType){
case "number":
case "text":
const questionType = document.createElement('input');
questionType.id = getObject[i].id;
questionType.type = getObject[i].input;
questionText.append(questionType);
break;
//create text characters
case "single-select":
const options = getObject[i].options;
options.array.forEach(option => {
const questionOptionText = document.createElement('span');
questionOptionText.textContent = option;
const questionOption = document.createElement('input');
questionOption.type = 'radio';
questionOption.value = option;
questionOption.name = getObject[i].id;
questionText.append(questionOption, questionOptionText)
});
break;
case "multi-select":
const newOptions = getObject[i].options;
newOptions.array.forEach(options => {
const optionText = document.createElement('span');
optionText.textContent = option;
const questionOption = document.createElement('input');
questionOption.type = 'checkbox';
questionOption.value = option;
questionOption.name = getObject[i].id;
questionText.append(questionOption, optionText)
});
break;
}
card.appendChild(questionText);
}
createButtons();
}
function createButtons(){
const submitButton = document.createElement('input');
const retrieveButton = document.createElement('input');
submitButton.type = 'submit';
retrieveButton.type = 'submit';
contents.append(submitButton, retrieveButton);
submitButton.addEventListener("click", submitData);
if(submitButton.clicked == true){
console.log("clicked");
}
}
And this is the json data in the json file which I have fetched the data from into the javascript file:
{
"name": "Example Questionnaire",
"questions": [
{
"id": "name",
"text": "What is your name?",
"type": "text"
},
{
"id": "quest",
"text": "What is your quest?",
"type": "text"
},
{
"id": "col",
"text": "What is your favourite colour?",
"type": "text"
},
{
"id": "velo",
"text": "What is the air-speed velocity of an unladen swallow?",
"type": "number"
},
{
"id": "lord",
"text": "Which is the best lord?",
"type": "single-select",
"options": [
"Lord of the Rings",
"Lord of the Flies",
"Lord of the Dance",
"Lorde"
]
},
{
"id": "langs",
"text": "Which computer languages have you used?",
"type": "multi-select",
"options": [
"JavaScript",
"Java",
"C",
"Python",
"Ook",
"LISP"
]
}
]
}
When I run it this what my web page shows:
Does anyone know where I’m going wrong? I’m new to javascript and still trying to understand things. Any help is appreciated thank you