So I am creating MCQ options where each option is essentially a div with the class "qinp" inside of which there is a radio button. What I want to do is when the div is clicked, I want the radio button to get checked. There is an input of type radio inside every div with class "qinp". My HTML is something like this-
<div class="qinp">
<label class="label-option" for="q1o1">Option 1
<input type="radio" name="q1" id="q1o1">
<span class="rd-btn">A</span>
</label>
</div>
I am using Javascript and I am doing the following-
var number_of_qinps = document.querySelectorAll(".qinp").length;
for(var i = 0; i < number_of_qinps; ++i){
document.querySelectorAll(".qinp")[i].addEventListener("click", function(){
document.querySelectorAll(".qinp")[i].firstElementChild.firstElementChild.checked = true;
})
}
But for some reason I am getting the following error-
testpage.js:9 Uncaught TypeError: Cannot set property 'checked' of undefined at HTMLDivElement.
Can someone please explain what is going wrong here? This should have been fairly simple.