Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?


What I have tried:

<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>     I don't like tea."
            },

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

            },

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

            },

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

            },

            {
                answer: "5. They are not good. <br>     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>>
Posted
Updated 12-Apr-19 3:33am
Comments
MadMyche 10-Apr-19 22:00pm    
First thing to do is to use your browser's Developer Tools first on the network tab to make sure the JS file is loaded and then on the console tab to check for JS errors
Bryian Tan 12-Apr-19 9:48am    
Where do you place the JavaScript file? Show the markup.

1 solution

As mentioned in the comments, if it works fine inline but not in an external JavaScript file it means that you are either not loading the JavaScript file or you are not loading it at the right time/point in your main file.

Use the developer tools to see what is going on. Then it should be easy to fix.
 
Share this answer
 

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