This the code I wrote in order to find the answer to a programming challenge and it's to insert elements into a element with the values equal to color names and choosing a color form the list will change the color of the below it and it's supposed to be JS only. But my problem is that my code works perfectly on Firefox ,but it does not work in chrome or other browsers.
const color = [
"red",
"black",
"blue"
];
let select = document.getElementById("select");
for (let i = 0; i < color.length; i++) {
let container = document.createElement("option");
container.innerText = color[i];
container.value = color[i];
container.id = i+1;
select.append(container);
}
for (let j = 1; j <= color.length; j++) {
select[j].addEventListener("click", function colorChange() {
document.getElementById("box").style.backgroundColor = `${select[j].value}`;
});
}
#box {
width: 50px;
height: 50px;
border: 1px solid green;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<body>
<select name="" id="select">
<option value="">Choose a color</option>
</select>
<div id="box">
</div>
<script src="script.js">
</script>
</body>
</html>