In the task, you need to change the background colour of the block to green when you press the G key, but no changes are made.
<body>
<div id="colorOutput">
</div>
<script>
let div = document.querySelector("#colorOutput");
document.addEventListener("keydown", function (event) {
if (event.code == "Enter") changeToGreen();
})
function changeToGreen() {
div.style.backgroundСolor = "green";
}
</script>
Try using the below JS code.
const div = document.getElementById("colorOutput");
window.addEventListener("keydown", (e)=>{
e.code === "Enter" ? changeToGreen() : null;
});
function changeToGreen() {
alert("JAI HARI");
div.style.backgroundСolor = "green";
}
Use window instead of document for Event Listener.