user3259282 user3259282 - 5 months ago 10
Javascript Question

Input box to change the background color of a webpage using javascript

On my web page I have JavaScript script that changes the background color. My HTML is:

<a href="#" onclick="javascript:changeBGC('#FFFFFF')">White</a>


My Javascript function is:

function changeBGC(color){
document.bgColor = (color);
}


How can I change the color of the background getting the color from an
<input>
box instead of putting a predefined value?

Answer

Here's a FIDDLE

<input id="color" type="text" onKeyUp="color();">

function color() {
  document.body.style.background=document.getElementById('color').value;
}