I am trying to build a set of questions and answers for a questionnaire. Each instance has an id, a css class, a question, and at least one answer. Is it possible to have multiple values when there is more than one answer?
var qa = [
{id: "0", css: "multiple", question: "Do you own a home?", answers: "Yes", "No"},
{id: "1", css: "input", question: "Who will live in your home?", answer: "<textarea rows='5' class='textarea'></textarea>"}
];
You can do this by turning the answers in to an array:
var qa = [{
id: "0",
css: "multiple",
question: "Do you own a home?",
answers: ["Yes", "No"]
}];
And than access it like this:
qa[0].answers[0] // for "Yes"
qa[0].answers[1] // for "No"
or
qa[0]['answers'][0]// for "Yes"
qa[0]['answers'][1] // for "No"
Or instead of an array you also can use an object:
var qa = [{
id: "0",
css: "multiple",
question: "Do you own a home?",
answers: [yes: "Yes", no: "No"]
}];
And than access it like this:
qa[0].answers.yes // for "Yes"
qa[0].answers.no // for "No"
or
qa[0]['answers']['yes']// for "Yes"
qa[0]['answers']['no'] // for "No"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With