Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda