-1

I have created a JSON object which I want to display in a div: question and number of selections-format.

How to loop i.e. iterate this particular JSON object array in div?

[
    {
        "question": "php Developer ?",
        "type": "checkbox",
        "mandatory": "yes",
        "answer": [
            "a",
            "b",
            "c",
            "d"
        ],
        "answerss": [
            "a",
            "b",
            "c",
            "d"
        ]
    },
    {
        "question": "1+1",
        "type": "radio",
        "mandatory": "no",
        "answer": [
            "1",
            "2",
            "3",
            "4"
        ],
        "answerss": [
            "1",
            "2",
            "3",
            "4"
        ]
    }
]

like

Question
answer 1
answer 2
answer 3
answer 4
1

2 Answers 2

0

You need use javascript . You are using angular js ? if not .. Visit this link probably help you How do I dynamically populate html elements with JSON Data with Javascript not jQuery?

Sign up to request clarification or add additional context in comments.

Comments

0

Angular Js

You can achieve this using ng-repeat directive like -

<div ng-controller="MyCtrl">
  <div ng-repeat="question in questions">
      <div class="preview">Question - {{question.question}} </div>
         <div ng-repeat="answer in question.answerss">
           {{answer}}
         </div>
   </div>
</div>

Working jsfiddle - https://jsfiddle.net/z800015w/

Jquery

With Jquery you need to update innerHTML of the div by using following code -

$.each(questions, function(index, question){
    var newItem = "<div> Q - "+ question.question + "<div></br>Answer - </br>";
    $.each(question.answerss, function(index2, answer){
    newItem = newItem + answer+"</br>";
    });
    inHTML = inHTML + newItem+"</br>";  
});

Please find demo - https://jsfiddle.net/umd7nnhu/2/

1 Comment

is it not possible with jquery. because for angular js to work , i will be required to include one more angularjs library .and it will increase loading time .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.