Skip to content
Advertisement

How to use promptbox to change background color?

I found another post but it wasn’t using a promptbox. I just need the exact same functionality as this but using a promptbox:

function changeBackground() {
    var color = document.getElementById("color").value;
    document.bgColor = color;
}

document.getElementById("submitColor").addEventListener("click", changeBackground, false);

Advertisement

Answer

Use this to get the value of a prompt

var color = prompt("What color"); and this to set the background:

document.bgColor = color;

Now color will be whatever the user inputs.

Those two lines of code do the same thing as the above.

Thanks to @OliverRqdini heres a JS Fiddle

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement