Skip to content

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

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