How do I make the color of the box be the value of the input?
box = document.getElementById("box");
typeColor = document.getElementById("typeColor");
function changeBoxColor() {
box.style.backgroundColor = typeColor;
}
//box is a div, typeColor is an input
body {
margin: 0px;
}
#box {
width: 100px;
height: 100px;
background-color: grey;
margin-left: 100px;
margin-top: 50px;
}
<input id="typeColor" placeholder="Type a color...">
<button onclick="changeBoxColor()">Change Box Color</button>
<div id="box">
</div>
5
Answers
change color setting line as follows.
Here is an working code snippet
You can simply type the text on input box and in your code instead of typeColor please use the typeColor.value you can fill the box with the provided color name on that box.
Thank you.
The
value
property sets or returns the value of the value attribute of a input tag field.Syntax
Return the value property:
Set the value property:
Transfered to your code example:
More about that can be found here
Why make it simple when you can make it very complicated?