I want the typeColor input value to update the background color of the box. I want the typeName input value to update the text on the box. Instead, the color doesn't change at all and the name just disappears. I've tried various solutions.
box = document.getElementById("box");
typeColor = document.getElementById("typeColor").value;
typeName = document.getElementById("typeName").value;
boxName = document.getElementById("boxName");
function changeBoxColor() {
box.style.backgroundColor = typeColor;
}
function changeBoxName() {
boxName.innerText = typeName;
}
body {
margin: 0px;
}
#box {
width: 100px;
height: 100px;
background-color: grey;
margin-left: 100px;
margin-top: 50px;
display: flex;
justify-content: center;
align-items: center;
}
<head>
<title>test</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<input id="typeColor" placeholder="Type a color...">
<button onclick="changeBoxColor()">Change Box Color</button>
<input id="typeName" placeholder="Type a name...">
<button onclick="changeBoxName()">Change Box Name</button>
<div id="box">
<center>
<p id="boxName">Box</p>
</center>
</div>
<script src="script.js"></script>
</body>