0
Assuming your object has this structure, you can assemble the components and include them in a div
using innerHTML
:
var obj = {
"perguntas": [{
"campanhaId": 1,
"id": 1,
"nome": "pergunta 1",
"respostas": [
"alternativa A",
"alternativa B",
"alternativa C"
]
}, {
"campanhaId": 2,
"id": 1,
"nome": "pergunta 2",
"respostas": [
"alternativa A",
"alternativa B",
"alternativa C"
]
}]
};
var div = document.getElementById("perguntas")
, texto = '';
obj.perguntas.forEach(function(item){
texto += "<label>" + item.nome + "</label><br/>";
item.respostas.forEach(function(res){
texto += "<input type='radio' name='" +item.campanhaId+ "'>" + res + "</input><br/>";
});
});
div.innerHTML = texto;
<div id="perguntas"></div>
Thank you @Lucas, that’s exactly what I wanted to do.
– scooby