I am trying to create object for given set of Question
Each question contain set of answers which mat vary from minimum : 1 to maximum: 5.
HTML
<div id="displayAppendedQuestion" style="display: block;">
<div id="questionNumber1">
Question1 = Foo - 1 <br>
<input id="radioVal1.1" name="radioinput" type="radio" value="Answer 1.1 "> Answer 1.1 <br>
<input id="radioVal1.2" name="radioinput" type="radio" value="Answer 1.2"> Answer 1.2 <br>
<input id="radioVal1.3" name="radioinput" type="radio" value="Answer 1.3"> Answer 1.3 <br>
<input id="radioVal1.4" name="radioinput" type="radio" value="Answer 1.4"> Answer 1.4 <br>
<input id="radioVal1.5" name="radioinput" type="radio" value="Answer 1.5"> Answer 1.5 <br>
</div>
<div id="questionNumber2">
Question2 = Foo - 2 <br>
<input id="checkboxVal2.1" name="checkBoxInput" type="checkbox" value="Answer 2.1"> Answer 2.1 <br>
<input id="checkboxVal2.2" name="checkBoxInput" type="checkbox" value="Answer 2.2"> Answer 2.2 <br>
<input id="checkboxVal2.3" name="checkBoxInput" type="checkbox" value="Answer 2.3"> Answer 2.3 <br>
<input id="checkboxVal2.4" name="checkBoxInput" type="checkbox" value="Answer 2.4"> Answer 2.4 <br>
<input id="checkboxVal2.5" name="checkBoxInput" type="checkbox" value="Answer 2.5"> Answer 2.5 <br>
</div>
<div id="questionNumber3">
Question3 = Foo - 3 <br>
<input id="inputVal3" type="text" readonly="readonly" value="Answer 3.1"> <br>
</div>
<div id="questionNumber4">
Question2 = Foo - 4 <br>
<input id="checkboxVal4.1" name="checkBoxInput" type="checkbox" value="Answer 4.1"> Answer 4.1 <br>
<input id="checkboxVal4.2" name="checkBoxInput" type="checkbox" value="Answer 4.2"> Answer 4.2 <br>
<input id="checkboxVal4.3" name="checkBoxInput" type="checkbox" value="Answer 4.3"> Answer 4.3 <br>
<input id="checkboxVal4.4" name="checkBoxInput" type="checkbox" value="Answer 4.4"> Answer 4.4 <br>
<input id="checkboxVal4.5" name="checkBoxInput" type="checkbox" value="Answer 4.5"> Answer 4.5 <br>
</div>
</div>
Question:
How to create object for given set of input using javascript.
JavaSript
I have no of total Questions = totalQuestions
// create object for
for ( var i = 1 ; i < totalQuestions ; i++){
inputs = document.getElementById('questionNumber'+i).getElementsByTagName('input');
var obj = {
question: ("QUESTION_" + document.getElementById('Ques').value),
answertype: ("QTYPE_" + document.getElementById('surAnsType').value)
for (var j = 0, l = inputs.length; j < l; ++j) {
if (inputs[j].value.length) {
options:option_arr.push(inputs[j].value);
}
}
};
arrayOfObject.push(obj);
}
totalQuestions-1? Your loop will only process the last question, not all the questions.id="questionNumber4"?