codice:
<head>
<style>
#TheForm {
margin-left: 396px;
}
#submitColor {
margin-left: 48px;
margin-top: 5px;
}
#coltext{width:200px;
height:40px;
border:1px solid black;
}
</style>
<script>
function changeBackground() {
var color = document.getElementById("color").value.trim(),
rxValidHex = /^#(?:[0-9a-f]{3}){1,2}$/i;
if (rxValidHex.test(color)) {
document.getElementById("coltext").style.background = color;
} else {
alert("solo colori esadecimali");
}
}
document.getElementById("submitColor").addEventListener("click", changeBackground, false);
</script>
</head>
<body>
<form id="TheForm">
<input id="color" type="text" />
<input id="submitColor" value="Submit" type="button" />
</form>
<div id="coltext">ciao ciao test test test test</div>
</body>
ESEMPIO
Puoi usare solo valori esadecimali, ovvero #000 , #fff, #f00 ecc...