2

I'm making a quiz with a text input. This is what I have so far:

<html>
    <head>
        <script type="text/javascript">
            function check() {
                var s1 = document.getElementsByName('s1');
                if(s1 == 'ō') {
                    document.getElementById("as1").innerHTML = 'Correct';
                } else {
                    document.getElementById("as1").innerHTML = 'Incorrect';
                }
                var s2 = document.getElementsByName('s2');
                if(s2 == 's') {
                    document.getElementById("as2").innerHTML = 'Correct';
                } else {
                    document.getElementById("as2").innerHTML = 'Incorrect';
                }
                //(...etc...)
                var p3 = document.getElementsByName('p3');
                if(p3 == 'nt') {
                    document.getElementById("ap3").innerHTML = 'Correct';
                } else {
                    document.getElementById("ap3").innerHTML = 'Incorrect';
                }
            }
        </script>
    </head>
    <body>
        1st sing<input type="text" name="s1"> <div id="as1"><br>
        2nd sing<input type="text" name="s2"> <div id="as2"><br>
        <!-- ...etc... -->
        3rd pl<input type="text" name="p3"> <div id="ap3"><br>
        <button onclick='check()'>Check Answers</button>
    </body>
</html>

Every time I check answers it always says Incorrect and only shows the first question. I also need a way to clear the text fields after I check the answers. One of the answers has a macro. Thanks in advance.

Matthijs Wessels
  • 6,530
  • 8
  • 60
  • 103
sears
  • 79
  • 1
  • 1
  • 8

2 Answers2

3

The method getElementsByName returns a NodeList, you can't really compare that against a string. If you have only one element with such name, you need to grab the first element from that list using such code instead:

var s1 = document.getElementsByName('s1')[0].value;

To make it more flexible and elegant plus avoid error when you have typo in a name, first add such function:

function SetStatus(sName, sCorrect, sPlaceholder) {
    var elements = document.getElementsByName(sName);
    if (elements.length == 1) {
        var placeholder = document.getElementById(sPlaceholder);
        if (placeholder) {
            var value = elements[0].value;
            placeholder.innerHTML = (value === sCorrect) ? "Correct" : "Incorrect";
        } else {
            //uncomment below line to show debug info
            //alert("placeholder " + sPlaceholder+ " does not exist");
        }
    } else {
        //uncomment below line to show debug info
        //alert("element named " + sName + " does not exist or exists more than once");
    }
}

Then your code will become:

SetStatus('s1', 'ō', 'as1');
SetStatus('s2', 's', 'as2');
//...
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • Still only seeing first question after clicking button. Here is my check function: `function check() { SetStatus('s1', 'ō', 'as1'); SetStatus('s2', 's', 'as2'); SetStatus('s3', 't', 'as3'); SetStatus('p1', 'mus', 'ap1'); SetStatus('p2', 'tis', 'ap2'); SetStatus('p3', 'nt', 'ap3'); }` – sears Nov 12 '12 at 15:26
  • @sks you need to close your `
    ` tags, e.g. `
    `. Your current HTML is invalid.
    – Shadow The GPT Wizard Nov 12 '12 at 15:27
0

document.getElementsByName('s1') is an array you should use document.getElementsByName('s1')[0] to get certain element(first in this case)

dav
  • 8,931
  • 15
  • 76
  • 140
  • 2
    That's actually a NodeList not an array. You access it the same way, but you can't use array functions on it. (pop, push, etc) "TypeError: Object # has no method 'pop'" – tbwiii Nov 12 '12 at 15:42