Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array of questions and answers. I just want that if the user types the right answer in the text field, the background color of the text field gets changed without any click. I have difficulty writing the code of checkAnswer function. Could you please help me write the correct code ?


What I have tried:

<body>

    <div id="container">

        <h1> Press 'Enter' after writing your answer. </h1>

        <form>

            <input type="text" class="input" name="questions[]" />

            <input type="text" class="output" />

            <input type="text" class="input" name="questions[]" />

            <input type="text" class="output" />

            <input type="text" class="input" name="questions[]" />

            <input type="text" class="output" />

            <input type="text" class="input" name="questions[]" />

            <input type="text" class="output" />

            <input type="text" class="input" name="questions[]" />

            <input type="text" class="output" />

        </form>

        <a id="sub" href="#"> Submit </a>

        <div id="result"> </div>

    </div>

</body>


<script>
    var questions = [

        {
            question: "I like tea.",
            answer: "I do not like tea.",
            ans: "I don't like tea."
        },

        {
            question: "You are my friend.",
            answer: "You are not my friend.",
            ans: "You aren't my friend."
        },

        {
            question: "She is very naughty.",
            answer: "She is not very naughty",
            ans: "She isn't very naughty."
        },

        {
            question: "You can do it.",
            answer: "You cannot do it",
            ans: "You can't do it."
        },

        {
            question: "They are good.",
            answer: "They are not good.",
            ans: "They aren't good."
        },

    ];



    $('document').ready(function() {

        $.each(questions, function(i, x) {

            $('.input').eq(i).val(x.question);

            $('.input').prop('readonly', true);

        });

    });


    <pre>  function checkAnswer() {

        for (i = 0; i < questions.length; i++) {

            let realanswer = questions[i];

            let useranswer = $('.output').val();

            if (realanswer.answer == useranswer) {

                $(".output").style.background = "green";

            }

        }

        checkAnswer();

    }

</script>
Posted
Updated 9-Apr-19 12:03pm
v2

1 solution

Try using this code instead:

JavaScript
$('document').ready(function() {

    $.each(questions, function(i, x) {

        $('.input').eq(i).val(x.question);

        $('.output').eq(i).attr("answer", x.answer);

        $('.input').prop('readonly', true);

    });

    $(".output").on("keyup", function (){
        checkAnswer(this);
    });

});


function checkAnswer(e) {

    let useranswer = $(e).val();

    let realanswer = $(e).attr("answer");

    if (realanswer == useranswer) {
        $(e).css("background-color", "green");
    } else {
        $(e).css("background-color", "white");
    }

}


This will add an attribute to the all the ""s with "output" class and then it will run the "checkAnswer" function "keyup" for those ""s to see if the answer is correct or not.
 
Share this answer
 
Comments
Tarun Rathore 10-Apr-19 8:19am    
Great ! Thanks a lot for your help.
Tarun Rathore 10-Apr-19 17:13pm    
Great! Thanks a lot for your help. It worked well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900