Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!