I have a json file:
{
"questions": [
{
"title": "01. q1",
"type": "dropdown",
"options": [
{
"1",
"2",
"3"
}
]
},
{
"title": "q2",
"type": "multiple_choice",
"options": [
"1",
"2",
"3",
"4",
"5"
]
},
And I'm trying to write a for loop in a js file in order to go through the data, and if the question type is "dropdown", display an html dropdown selectors, "multiple choice" would get buttons, etc:
console.log = function(message) {
document.getElementById('q_1').innerHTML = message;
};
console.log2 = function(message) {
document.getElementById('q_2').innerHTML = message;
};
$.getJSON("generated.json", function(json)) {
for (var i = 0; i<json.questions.length; i++) {
if(json.questions[i].type=="drop_down") {
}
}
}
I'm really confused as to how to do this dynamically and not hard code most of the code. Is there a way for this to be written concisely without having to write out every option/question in an html file and just read and generate questions from the json file?
function(json))<-- remove the 2nd)and put it after the final}. Other than that it looks fine. Incidentally, did you really want to override the console log method??