sears
sears

Reputation: 79

How do I compare the value of an element returned by getElementsByName to a string?

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.

Upvotes: 2

Views: 1604

Answers (2)

Shadow Wizzard
Shadow Wizzard

Reputation: 66398

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');
//...

Upvotes: 3

dav
dav

Reputation: 9277

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

Upvotes: 0

Related Questions