May 30, 2013

Javascript not checking whether a checkbox was selected or not

User2437473’s Question:

I’m trying to build a quiz with multiple choice questions, one of which has multiple correct answers. So I’m trying to check which checkboxes in my questions have been selected by a student in order to give the right feedback. my code is:

for(var i = 0; i< input1.length; i++)
        if(input1[0].checked && input1[1].checked)
        submit_answer.onclick = showFeedback1; 
        submit_answer.onclick = false1; 

It never takes the first if, even if I select those two only. No matter what I put in the if statement, it only takes the else.

and this is just a part of my .js

var quiz = document.getElementById('quiz');

var questions = quiz.getElementsByTagName('p');

input1 = questions[0].getElementsByTagName('input');

var submit_answer = document.getElementById('submit_answers'); // this is the submit button

I cannot make proper assumption of what you are trying to do.


Your for loop is incrementing on 1, so on the each next iteration it is comparing using same previously used value.


Your structure is horrible, your script fetches up all the input elements inside every p. You should properly organize your element in groups and then match whether or not they are checked.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!