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>
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>