Tariq
Tariq

Reputation: 2569

RadioButton always returning false - JavaScript

I have a HTML page that have many section with inputs consisting of RadioButtons and CheckBox. The first one of them is like this:

<div  style="margin-top: 30px; height: 270px;">
    <input id="Answer1" type="radio" name="Answers" title="True" onclick="setAnswered();"/>True<br/>
    <input type="radio" name="Answers" title="False" onclick="setAnswered();" />False
</div>
<div id="replaceAble1_a" style="display: none;">Answer1</div>

After the final input I am looking for each input and investigating that the correct answers are checked. Here is the part of script that is doing this job:

var ansList = document.getElementById(thisFrame + "_a").innerHTML.split(",");
for (var k = 0; k < ansList.length; k++) {
   var ele =  document.getElementById(ansList[k]);
   result = result & ele.checked ;
}

where thisFrame for this iteration is replaceAble1. Upon debugging it is clear that I am getting the correct element id in ansList[k] i.e. Answer1. Now the problem is, every time, whether the Answer1 is checked or not ele.checked is returning false. What could be the reason?

Update:

I think it would better to put complete code. So here it is:

<div id="replaceAble1">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q 
        <span id ="question1">Do you live in Pakistan?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input id="Answer1" type="radio" name="Answers" title="True" onclick="setAnswered();"/>True<br/>
        <input type="radio" name="Answers" title="False" onclick="setAnswered();" />False
    </div>
    <div id="replaceAble1_a" style="display: none;">Answer1</div>
    <div id="replaceAble1_n" style="display: none;">null</div>
</div>
<div id="replaceAble2" style="display: none;">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q. 
        <span id ="question2">Do you go for cycling daily?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input type="radio" name="Answers" title="True" onclick="setAnswered();" />True<br/>
        <input id="Answer2" type="radio" name="Answers" title="False" onclick="setAnswered();" />False
    </div>
    <div id="replaceAble2_a" style="display: none;">Answer2</div>
    <div id="replaceAble2_n" style="display: none;">null</div>
</div>
<div id="replaceAble3" style="display: none;">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q 
        <span id ="question3">When Do you Sleep?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input id="notAnswer1" type="checkbox" name="Answers" title="True" onclick="setAnswered();"/>Morning<br/>
        <input id="notAnswer2" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Evening<br/>
        <input id="Answer3" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Night                        
    </div>
    <div id="replaceAble3_a" style="display: none;">Answer3</div>
    <div id="replaceAble3_n" style="display: none;">notAnswer1,notAnswer2</div>
</div>
<div id="replaceAble4" style="display: none;">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q 
        <span id ="question4">What Do you Eat?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input id="Answer4" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Launch<br/>
        <input id="Answer5" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Break Fast<br/>
        <input id="Answer6" type="checkbox" name="Answers" title="False" onclick="setAnswered();" />Dinner<br/>
    </div>
    <div id="replaceAble4_a" style="display: none;">Answer4,Answer5,Answer6</div>
    <div id="replaceAble4_n" style="display: none;">null</div>
</div>

And the script is:

var i = 0;
var answered = false;
var score = 0;
var unitScore = 5;
var totalScore = 20;
var frameList = ["replaceAble1", "replaceAble2", "replaceAble3", "replaceAble4"];
function setAnswered() {
    answered = true;
}
function displayAnswer(isTimeUp) {
    var Message;
    var j;
    if (isTimeUp) {
        Message = "Times Up";
    } else {
        Message = "Quiz End";
    }

    for (j = 0; j < frameList.length; j++) {
        var result = true;
        var thisFrame = frameList[j];
        var ansList = document.getElementById(thisFrame + "_a").innerHTML.split(",");
        for (var k = 0; k < ansList.length; k++) {
            var ele =  document.getElementById(ansList[k]);
            result = result & ele.checked ;
        }
        ansList = document.getElementById(thisFrame + "_n").innerHTML.split(",");
        for (var k = 0; k < ansList.length; k++) {
            if (ansList[k] !== "null")
                result = result & (!document.getElementById(ansList[k]).checked);
        }
        if (result) {
            score = Number(score) + Number(unitScore);
        }
    }
    document.getElementById("QuestionPan").style.display = "none";
    document.getElementById("resultPan").style.display = "block";
    document.getElementById("message").innerHTML = Message;
    document.getElementById("totalMarks").innerHTML = totalScore;
    document.getElementById("score").innerHTML = score;
    var timetaken = count - document.getElementById("time").innerHTML;
    document.getElementById("timetaken").innerHTML = timetaken + " minutes";
}
function submition() {
    if (!answered) {
        alert("Please select at least one Answer for this question.");
        return;
    }
    if (i === (frameList.length - 1)) {
        displayAnswer(false);
        return;
    }
    document.getElementById(frameList[i++]).style.display = "none";
    document.getElementById(frameList[i]).style.display = "block";
    document.getElementById("counter").innerHTML = (Number(i) + 1) + '/' + frameList.length;
    answered = false;
}

Only the first answer i.e. Answer1 is causing this problem. Every thing else is working fine.

Upvotes: 0

Views: 1948

Answers (2)

Tariq
Tariq

Reputation: 2569

Got the problem. The problem was with HTML not the JavaScript. I have by mistake set the name field of all inputs same i.e. Answer. Hence all the radio button in all replaceable divs were belonging to same group. So when I set the Answer2 of replaceable2 the radio button of replaceable1 is reset. Being invisible I was't able to track this bug. So simply I changed the html to

<div id="replaceAble1">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q 
        <span id ="question1">Do you live in Pakistan?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input id="Answer1" type="radio" name="Answers1" title="True" onclick="setAnswered();"/>True<br/>
        <input type="radio" name="Answers1" title="False" onclick="setAnswered();" />False
    </div>
    <div id="replaceAble1_a" style="display: none;">Answer1</div>
    <div id="replaceAble1_n" style="display: none;">null</div>
</div>
<div id="replaceAble2" style="display: none;">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q. 
        <span id ="question2">Do you go for cycling daily?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input type="radio" name="Answers2" title="True" onclick="setAnswered();" />True<br/>
        <input id="Answer2" type="radio" name="Answers2" title="False" onclick="setAnswered();" />False
    </div>
    <div id="replaceAble2_a" style="display: none;">Answer2</div>
    <div id="replaceAble2_n" style="display: none;">null</div>
</div>
<div id="replaceAble3" style="display: none;">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q 
        <span id ="question3">When Do you Sleep?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input id="notAnswer1" type="checkbox" name="Answers3" title="True" onclick="setAnswered();"/>Morning<br/>
        <input id="notAnswer2" type="checkbox" name="Answers3" title="False" onclick="setAnswered();" />Evening<br/>
        <input id="Answer3" type="checkbox" name="Answers3" title="False" onclick="setAnswered();" />Night                        
    </div>
    <div id="replaceAble3_a" style="display: none;">Answer3</div>
    <div id="replaceAble3_n" style="display: none;">notAnswer1,notAnswer2</div>
</div>
<div id="replaceAble4" style="display: none;">
    <span  style="font-size: 14pt ;margin-top: 5px;">Q 
        <span id ="question4">What Do you Eat?</span>
    </span>
    <div  style="margin-top: 30px; height: 270px;">
        <input id="Answer4" type="checkbox" name="Answers4" title="False" onclick="setAnswered();" />Launch<br/>
        <input id="Answer5" type="checkbox" name="Answers4" title="False" onclick="setAnswered();" />Break Fast<br/>
        <input id="Answer6" type="checkbox" name="Answers4" title="False" onclick="setAnswered();" />Dinner<br/>
    </div>
    <div id="replaceAble4_a" style="display: none;">Answer4,Answer5,Answer6</div>
    <div id="replaceAble4_n" style="display: none;">null</div>

And it worked properly.

Upvotes: 0

Phil
Phil

Reputation: 164811

You probably want to use the boolean && operator, not the bitwise & operator.

result = result && ele.checked;

This also depends on the previous value of result. I'm not entirely sure this is your problem at this stage.

Update

I can't reproduce the problem. See http://jsfiddle.net/QA26Q/

My only other thought is that you have multiple radio buttons with the same ID as shown in this example - http://jsfiddle.net/QA26Q/1/.

Upvotes: 2

Related Questions