Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
how to reprompt the user (or loop a prompt), until user terminates programs in javascript?

I am currently making a simple html page with javascript code where I have to prompt the user with a question to fill in a color-name which changes the background of the page after to the color of the user-input.

If that's done, I'd like to continuously display the prompt on the page so that the user can keep entering color-names until he wishes to exit the page or terminate the program.

It has to be written in the simplest/least amount of codelines as possible, and with the use of "prompt()" and "background.style.bgcolor".

Do you know how to do it?

<!DOCTYPE html>
<html lang="en">
<title> Background Colors</title>
<body onload="changeColor()" ontimeupdate="userInput" load="3sec">
<h1> <b style="text-align: center;">Background Color Changer</b> </h1>
<form name = "myform">
</form>
</body>
<script type="text/javascript" style="align-items: center; justify-content: center">
    function changeColor()
    {
    var userInput = window.prompt("Enter a background-color:");
    document.bgColor = userInput;
    }
</script>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Prompt the user for a colour. If the prompt returns a colour, change the background of the document body, and call the function again. If the prompt returns an empty string, or the user clicks cancel, don't call the function.

const body = document.body;

function changeColor() {
  const color = prompt('Enter a color.');
  if (color) {
    body.style.backgroundColor = color;
    changeColor();
  }
}

changeColor();
<h3>Background Color Changer</h3>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda