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

0

168
Views
Cambiar el fondo del color del formulario al hacer clic en enviar usando Java

tengo este formulario

ingrese la descripción de la imagen aquí

Lo que debo hacer es cambiar el color del fondo del formulario a rojo si es incorrecto o verde si es correcto, cuando se hace clic en "verificar respuesta".

esto es lo que tengo hasta ahora:

JavaScript

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

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

 }
 

HTML

 <hr>
 ¿Cuál será el edificio más alto del mundo en 2022? <!--BURJ KHALIFA-->
 <formulario >
 <input style="text-transform: uppercase;" type="text" id="answer" name="answer">
 <input onclick="changecolor()" id="turnon" type="submit" value="Comprobar respuesta">{** *}

¿Puede alguien indicarme la dirección correcta? Gracias.

almost 3 years ago · Santiago Trujillo
3 answers
Answer question

0

Aquí está tu problema:

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

document.getElementById devolverá un Element o nulo/indefinido. Ninguno de estos será igual a una cadena

En cambio, quieres

 if (changec.value == "BURJ KHALIFA")
almost 3 years ago · Santiago Trujillo Report

0

En primer lugar, está llamando al método incorrecto en la entrada.

En segundo lugar, su declaración if en el método debe verificar changec.value y no solo changec.

Espero que esto pueda guiarte en la dirección correcta :D

almost 3 years ago · Santiago Trujillo Report

0

Actualice su condición de la siguiente manera

 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