• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

176
Views
Change form color background on submit click using java

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

almost 3 years ago · Santiago Trujillo
3 answers
Answer question

0

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")
almost 3 years ago · Santiago Trujillo Report

0

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

almost 3 years ago · Santiago Trujillo Report

0

Update your condition as below

if (changec.value == "BURJ KHALIFA")
almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error