0

I have this form

enter image description here

What i need to to do is to change the color of the form's background to either red if it's wrong or green if it's right, when the "check answer" is clicked.

this is what i have so far:

Javascript

function chagecolor()
        {
            var changec = document.getElementById("answer")
            if (changec == "BURJ KHALIFA")
            {
                changec.style.backgroundColor = '#F40320';

            }
            
            else
            {
                changec.style.backgroundColor = '#03C13D';

            }
            

HTML

            <hr>
            What's the tallest building in the world in 2022? <!--BURJ KHALIFA-->
            <form >
                <input style="text-transform: uppercase;" type="text" id="answer" name="answer">
                <input onclick="changecolor()" id="turnon" type="submit" value="Check Answer">

Can someone point me on the right direction Thanks

7
  • What's going wrong with the solution you've attempted? Commented Sep 29, 2022 at 16:47
  • @mykaf Nothing happens no color Commented Sep 29, 2022 at 16:48
  • 1
    update your condition like this if (changec.value == "BURJ KHALIFA") Commented Sep 29, 2022 at 16:53
  • 1
    Isn't function chagecolor() missing an n? Commented Sep 29, 2022 at 16:56
  • 1
    @j08691 your comment also helped me, thanks Commented Sep 29, 2022 at 17:02

3 Answers 3

1

Here is your problem:

        var changec = document.getElementById("answer")
        if (changec == "BURJ KHALIFA")

document.getElementById will return an Element or null/undefined. Neither of these will ever be equal to a String

Instead, you want

        if (changec.value == "BURJ KHALIFA")
Sign up to request clarification or add additional context in comments.

Comments

1

First of all you are calling the wrong method on the input.

Secondly your if statement in the method should check for changec.value and not only changec.

I hope this can guide you in the right direction :D

Comments

1

Update your condition as below

if (changec.value == "BURJ KHALIFA")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.