Condition in dict not push correctely
i have 2 type of questions in quiz plateforme, one choice with radiobutton and multichoice with checkbox, when the checkbox is in begin of quiz it's work , if it's in second question or in the last i have an error , and in my consol log the rep in not an array
in my html i have data who contain questions ans reps and type of reps:
<div class="repsCol">
{% for Question_reponse in Questions_reponses %}
<div class = "Question {% if forloop.first %}active{% endif %}">
<div class = "title_section_quiz">
<h2 class="qst">{{Question_reponse.question}}</h2>
<div class = "Reponses">
<div class = "img_quiz">
<img src="{% static 'img/sbg1.png' %}" width="300" height="300" alt="">
</div>
<div class = "reps_quiz">
{% for rep in Question_reponse.reps %}
{% if Question_reponse.question_type == 'normal' %}
<label class="container_radio" ><input type="radio" id = "chSub" class="subRep" name = "rep" value="{{rep}}">{{rep}}<span class="checkmark_radio"></span></label>
{% elif Question_reponse.question_type == 'multiples' %}
<label ><input type="checkbox" id = "chSub" class="subRep" name = "repCheck" value="{{rep}}">{{rep}}</label>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
{% endfor %}
<div class = "ButtonNext containerButNxt">
<input id = "ButNxt" type="button" onclick="Next()" value = "Question suivate">
</div>
</div>
and the function next() in jquery :
const allData = [];
function Next(){
//console.log(document.getElementsByClassName('Question active')[0].getElementsByTagName('p')[0].innerText);
//console.log(document.querySelector('input[name="rep"]:checked').value);
const resp = document.getElementsByClassName('Question');
const data = [] ;
var repcheck = [];
if(document.querySelector('input[name="rep"]:checked')){
data.push({
qst : document.getElementsByClassName('Question active')[0].getElementsByTagName('h2')[0].innerText,
rep : document.querySelector('input[name="rep"]:checked') .value
})
}
else if (document.querySelector('input[name="repCheck"]:checked')){
$('input[name="repCheck"]:checked').each(function(){
repcheck.push(this.value);
})
data.push({
qst : document.getElementsByClassName('Question active')[0].getElementsByTagName('h2')[0].innerText,
rep : repcheck
})
}
//console.log(data)
allData.push({...data});
for (let i = 0; i < resp.length; i++) {
let x = resp[i].className
if (x.search('Question active') !== -1 ) {
const reps = document.getElementsByClassName('subRep')
if (i < resp.length -1) {
document.getElementsByClassName(resp[i].classList.remove('active'))
document.getElementsByClassName(resp[++i].classList.add('active'))
}
else if (i === resp.length -1){
console.log(allData)
console.log('Quiz Términé !');
//////////////////////////////////////////////
$.ajax({
type: 'POST',
url: "{% url 'SUBQUIZ' id_quiz %}",
data: JSON.stringify({allData}),
headers: {
"X-Requested-With": "XMLHttpRequest",
"X-CSRFToken": getCookie("csrftoken")},
success: function (response) {
alert(response)
$('.Question').hide();
$('.ButtonNext').hide();
//window.location = "{% url 'AllQuiz' %}"
//$('.EndQuiz').css('display','block');
},
error: function (response) {
// alert the error if any error occured
alert('Un probléme wowwwwwwwwwww');
}
})
/////////////////////////////////////////////
}
}
}
}