Why a specific function does not work in javascript file while working well in html document?

I have two arrays of questions and answers. When the user types the right answer in the input field its background color changes. Everything is working fine if the script is in the html document but when it is in the javascript file, onclick function does not work. When the show answers button is clicked, the result div does not show, which is set to display: none. Could you please help me with finding the error ?

<body>
    <body onload="resetValues()">
        <div id="container">
            <h1> Welcome to English Pupils </h1>
            <form>
                <label> 1. <input type="text" class="input" /> </label>
                <input type="text" class="output" />
                <label> 2. <input type="text" class="input" /> </label>
                <input type="text" class="output" />
                <label> 3. <input type="text" class="input" /> </label>
                <input type="text" class="output" />
                <label> 4. <input type="text" class="input" /> </label>
                <input type="text" class="output" />
                <label> 5. <input type="text" class="input" /> </label>
                <input type="text" class="output" />
            </form>
            <a id="show" href="#result" onclick="printAnswers()"> Show Answers </a>
            <div id="result">
                <a id="hide" href="#container"> Hide Answers </a>
            </div>
        </div>
</body>

<style>
    #result {
        display: none;
    }
</style>
    <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."

            },

        ];

        // Answers printed in result div //  
        var answers = [

            {
                answer: "1. I do not like tea. <br> &nbsp&nbsp&nbsp I don't like tea."
            },

            {
                answer: "2. You are not my friend. <br> &nbsp&nbsp&nbsp You aren't my friend."

            },

            {
                answer: "3. She is not very naughty. <br> &nbsp&nbsp&nbsp She isn't very naughty."

            },

            {
                answer: "4. You cannot do it. <br> &nbsp&nbsp&nbsp You can't do it."

            },

            {
                answer: "5. They are not good. <br> &nbsp&nbsp&nbsp They aren't good."

            },

        ];

    // Showing questions in the input fields //

    $('document').ready(function() {
        $.each(questions, function(i, x) {
            $('.input').eq(i).val(x.question);
            $('.output').eq(i).attr("answer", x.answer);
            $('.output').eq(i).attr("ans", x.ans);
            $('.input').prop('readonly', true);

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

    // Printing Answers //

    function printAnswers() {
        for (i = 0; i < answers.length; i++) {
            $("#result").append(answers[i].answer + "<br>");
        }
        answers = [];
    }
        // Changing background color if the answer is right //

        function checkAnswer(e) {
            let useranswer = $(e).val();
            let realanswer = $(e).attr("answer");
            let real = $(e).attr("ans");
            if (realanswer == useranswer) {
                $(e).css("background-color", "#6ed66e");
            }

            if (real == useranswer) {
                $(e).css("background-color", "#6ed66e");
            }
            if ($(e).val() == "")
            {
                $(e).css("background-color", "white");
            }
        }

    // Empty answers fields on refresh //

    function resetValues() {
        var x = document.getElementsByClassName("output");
        for (i = 0; i <= x.length; i++)
        {
            x.item(i).value = "";
        }
    }
        // Showing Answers //    

        $("#show").click(function() {
            $("#result").css('display', 'flex');
            $("#show").hide();
        });

    // Hiding Answers //

    $("#hide").click(function() {
        $("#result").hide();
        $("#show").show();
    });

</script>>

You have posted many questions related to the same project. It would be best if used a single post for the entire project and used a Codepen link instead of copying/pasting your code each time. This will keep your project history in a single place and make it easier on you and others helping you to always have your latest code in front of them.

1 Like

Have you tried binding the on click functions like this

$(document).on(‘click’, $(’#show’), function(){
$("#result").css(‘display’, ‘flex’);
$("#show").hide();
});
});

$(document).on(‘click’, $(’#hide’), function(){
$("#result").hide();
$("#show").show();
});
});

What is this codepen link ?

You can go to Codepen.io and create pens.

1 Like