jeudi 4 mai 2017

A javascript function, using a loop, if and logic (&&)

I am using a javascript function with four checkboxes, two of which are the right answer. I have attempted to use the following code.

function q4() {
var a4 = document.getElementsByName('q4');
    for(i=0; i < a4.length; i++) {
        if(a4[i].checked) {
            if(a4[i].value == "a" && a4[i].value == "d") {
            correctAnswers++;
            alert('This works');
            }
        }// end if first if
    }// end of for loop

}// end of function q4

This is to supposed to produce an alert if the values a AND d are correct.

The HTML:

<!-- Question 4 -->  
    <div class="question-box">  
        <div class="question"><h3>Question Four: Identify two output devices from the images.</h3></div>
        <div class="answers">  
            <input type="checkbox" name="q4" value="a">A)&nbsp;<img src="imgs/printer.jpg" alt="question image"><br/>
            <input type="checkbox" name="q4" value="b">B)&nbsp;<img src="imgs/keyboard.jpg" alt="question image"><br/>
            <input type="checkbox" name="q4" value="c">C)&nbsp;<img src="imgs/scanner.jpg" alt="question image"><br/>
            <input type="checkbox" name="q4" value="d">D)&nbsp;<img src="imgs/speakers.jpg" alt="question image"><br/>
        </div>
        <br>
    <input type="button" name="submit" value="Check Answer" onclick="q4()">
    <h3 id="Answer4">&nbsp;</h3>
    </div><!-- end of question four -->




Aucun commentaire:

Enregistrer un commentaire